finished 32 bit stuff

This commit is contained in:
Jack Halford 2017-10-30 12:26:34 +01:00
parent f57c2acc46
commit 33b7b25abe
28 changed files with 857 additions and 203 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/26 19:15:02 by jhalford ### ########.fr # # Updated: 2017/10/30 12:22:10 by jhalford ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #
@ -34,21 +34,27 @@ NM_OBJ = $(OBJ_DIR)ft_nm.o
OTOOL_OBJ = $(OBJ_DIR)ft_otool.o OTOOL_OBJ = $(OBJ_DIR)ft_otool.o
SRC_BASE = \ SRC_BASE = \
ft_otool.c\ dump_symtab.c\
mach_64/symbol_64_init.c\
mach_64/symbol_64_filter.c\
mach_64/symbol_64_sort.c\
mach_64/mach_64.c\
mach_64/symbol_64_free.c\
mach_64/symbol_64_format.c\
mach_32/symbol_32_free.c\
mach_32/symbol_32_sort.c\
mach_32/mach_32.c\
mach_32/symbol_32_format.c\
mach_32/symbol_32_filter.c\
mach_32/symbol_32_init.c\
ft_nm.c\ ft_nm.c\
dump_symtab.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\
mach/symbol_free.c\
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
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE)) SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $($(notdir SRC_BASE):.c=.o)) OBJS = $(addprefix $(OBJ_DIR), $($(notdir SRC_BASE):.c=.o))
@ -57,22 +63,12 @@ OBJS := $(filter-out $(OTOOL_OBJ), $(OBJS))
NB = $(words $(SRC_BASE)) NB = $(words $(SRC_BASE))
INDEX = 0 INDEX = 0
MACH_64_SRC := $(wildcard $(SRC_DIR)mach_64/*)
MACH_64_SRC += $(INC_DIR)mach_64.h
MACH_SRC := $(subst _64,_32, $(MACH_64_SRC))
SHELL := /bin/bash SHELL := /bin/bash
all: all:
@make -C $(LIBFT_DIR) @make -C $(LIBFT_DIR)
@make -j $(NAME) @make -j $(NAME)
# $(SRC_DIR)/mach/%.c: $(SRC_DIR)/mach_64/%.c
$(MACH_SRC):
@mkdir -p $(SRC_DIR)/mach_32
@echo "$@ ---> $(subst _32,_64, $@)"
@sed s/_64/_32/g $(subst _32,_64, $@) > $@
ft_nm: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(NM_OBJ) ft_nm: $(LIBFT_LIB) $(OBJ_DIR) $(OBJS) $(NM_OBJ)
@$(CC) $(OBJS) -o $@ \ @$(CC) $(OBJS) -o $@ \
-I $(INC_DIR) \ -I $(INC_DIR) \
@ -129,6 +125,6 @@ re: fclean all
relib: fcleanlib $(LIBFT_LIB) relib: fcleanlib $(LIBFT_LIB)
.PHONY : fclean clean re relib cleanlib fcleanlib $(MACH_SRC) .PHONY : fclean clean re relib cleanlib fcleanlib
-include $(OBJS:.o=.d) -include $(OBJS:.o=.d)

5
nm-otool/gen_mach.sh Executable file
View file

@ -0,0 +1,5 @@
#!/bin/sh
for file in $@; do
sed s/_64//g $@ > $(sed s/_64//g <<< "$@")
done

95
nm-otool/includes/ft_nm.h Normal file
View file

@ -0,0 +1,95 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_nm.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:44:40 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:00:49 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_NM_H
# define FT_NM_H
/*
** sorting flags
** -r reverse sort
** -n numerical sort (as opposed to alphabetical)
** -p don't sort
*/
# define NM_NOSORT (1 << 0)
# define NM_NSORT (1 << 1)
# define NM_ASORT (1 << 2)
# define NM_RSORT (1 << 3)
/*
** filtering flags
** -a show all symbols
** -g filter-out local symbols
** -U filter-out undefined symbols
** -u show only undefined symbols
*/
# define NM_ALL (1 << 4)
# define NM_NO_LOCAL (1 << 5)
# define NM_NO_UNDF (1 << 6)
# define NM_ONLY_UNDF (1 << 7)
/*
** formating flags
** -o prepend file name on each line
** -m Display the N_SECT type symbols (Mach-O symbols) as (segment_name,
** section_name) followed by either external or non-external and then
** the symbol name. Undefined, common, absolute and indirect symbols
** get displayed as (undefined), (common), (absolute), and (indirect),
** respectively.
*/
# define NM_FULL (1 << 8)
# 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,
SYM_ABS,
SYM_TEXT,
SYM_DATA,
SYM_BSS,
SYM_COMMON,
SYM_STAB,
SYM_OTHER,
SYM_INDR,
};
struct s_machodata
{
void *file;
t_list *sects;
t_list *symbols;
struct symtab_command *symtab;
struct dysymtab_command *dysymtab;
};
struct s_symbolmap
{
char c;
char *s;
};
#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/10/26 18:42:47 by jhalford ### ########.fr */ /* Updated: 2017/10/30 12:00:14 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -25,95 +25,15 @@
# include <mach-o/nlist.h> # include <mach-o/nlist.h>
# include <mach-o/fat.h> # include <mach-o/fat.h>
# include "ft_nm.h"
# include "ft_otool.h"
# include "mach_64.h"
# include "mach.h"
# define IS_MACH_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64) # 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_MACH_32(x) (x == MH_MAGIC || x == MH_CIGAM)
# define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM) # define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM)
/*
** sorting flags
** -r reverse sort
** -n numerical sort (as opposed to alphabetical)
** -p don't sort
*/
# define NM_NOSORT (1 << 0)
# define NM_NSORT (1 << 1)
# define NM_ASORT (1 << 2)
# define NM_RSORT (1 << 3)
/*
** filtering flags
** -a show all symbols
** -g filter-out local symbols
** -U filter-out undefined symbols
** -u show only undefined symbols
*/
# define NM_ALL (1 << 4)
# define NM_NO_LOCAL (1 << 5)
# define NM_NO_UNDF (1 << 6)
# define NM_ONLY_UNDF (1 << 7)
/*
** formating flags
** -o prepend file name on each line
** -m Display the N_SECT type symbols (Mach-O symbols) as (segment_name,
** section_name) followed by either external or non-external and then
** the symbol name. Undefined, common, absolute and indirect symbols
** get displayed as (undefined), (common), (absolute), and (indirect),
** respectively.
*/
# define NM_FULL (1 << 8)
# 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,
SYM_ABS,
SYM_TEXT,
SYM_DATA,
SYM_BSS,
SYM_COMMON,
SYM_STAB,
SYM_OTHER,
SYM_INDR,
};
struct s_machodata
{
void *file;
t_list *sects;
t_list *symbols;
struct symtab_command *symtab;
struct dysymtab_command *dysymtab;
};
struct s_symbolmap
{
char c;
char *s;
};
# include "mach_64.h"
void dump_dysymtab(t_machodata *data,
struct dysymtab_command *dysymtab);
extern t_symbolmap g_symbolmap[];
extern t_machodata *g_data;
#endif #endif

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_otool.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:46:23 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:00:39 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef FT_OTOOL_H
# define FT_OTOOL_H
/*
** otool flags
** -t show text section
** -d show data section
*/
# define OTOOL_TEXT (1 << 0)
# define OTOOL_DATA (1 << 1)
typedef t_data_template t_otooldata;
#endif

44
nm-otool/includes/mach.h Normal file
View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mach.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:17:17 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef MACH_H
# define MACH_H
typedef struct s_symbol t_symbol;
struct s_symbol
{
int pos;
t_symtype type;
char *string;
struct nlist *nlist;
struct section *section;
};
int symbol_init(t_symbol *symbol,
char *stringtable, struct nlist *array, int i);
int symbol_set(t_symbol *symbol, t_machodata *data);
void symbol_free(void *data, size_t size);
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(void *file, char *segname,
char *sectname);
void mach_parse(t_machodata *data);
void nm_mach(struct mach_header *file,
t_nmdata *data);
void otool_mach(void *file, t_otooldata *data);
#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/10/26 18:43:32 by jhalford ### ########.fr */ /* Updated: 2017/10/30 12:22:12 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,7 +33,12 @@ 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_nmdata *data);
void mach_64_dump(struct mach_header_64 *file, void *get_section_64(void *file, char *segname,
char *sectname);
void mach_64_parse(t_machodata *data);
void nm_mach_64(struct mach_header_64 *file,
t_nmdata *data); t_nmdata *data);
void otool_mach_64(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/03/14 20:22:56 by jhalford #+# #+# */ /* Created: 2017/03/14 20:22:56 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:04:53 by jhalford ### ########.fr */ /* Updated: 2017/10/30 11:54:42 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/14 15:34:21 by jhalford #+# #+# */ /* Created: 2017/03/14 15:34:21 by jhalford #+# #+# */
/* Updated: 2017/05/15 17:37:29 by ariard ### ########.fr */ /* Updated: 2017/10/30 10:06:10 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,7 @@
*/ */
# define DG_MSG "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}" # define DG_MSG "{inv}{ran}%5i{yel}%21s {bol}{blu}%-3d{eoc}"
# define DG_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__ # define DG_ARGS getpid(), getpid(), ft_path_notdir(__FILE__), __LINE__
# define DG(s, ...) ft_dprintf(STDBUG,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__) # define DG(s, ...) ft_dprintf(STDERR,DG_MSG s "{eoc}\n",DG_ARGS,##__VA_ARGS__)
/* /*
** DEBUG with no malloc ** DEBUG with no malloc

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */ /* Created: 2016/11/07 13:49:04 by jhalford #+# #+# */
/* Updated: 2017/10/07 18:07:31 by jhalford ### ########.fr */ /* Updated: 2017/10/30 11:25:37 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -89,7 +89,8 @@ int ft_putendl_fd(char const *s, int fd);
int ft_putnbr_fd(long n, int fd); int ft_putnbr_fd(long n, int fd);
int ft_putnbr_hex_fd(long n, int fd); int ft_putnbr_hex_fd(long n, int fd);
void *hexdump(void *addr, unsigned int offset, unsigned int size); void *hexdump(void *addr, uint32_t offset, uint32_t size);
void *hexdump_64(void *addr, uint64_t offset, uint64_t size);
void *ft_realloc(void *data, int size); void *ft_realloc(void *data, int size);

View file

@ -6,21 +6,21 @@
/* 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/10/07 17:56:40 by jhalford ### ########.fr */ /* Updated: 2017/10/30 11:25:51 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h> #include <stdio.h>
#include "libft.h" #include "libft.h"
static void print_hex_contents(void *addr, unsigned int size) static void print_hex_contents(void *addr, uint64_t size)
{ {
void *a; void *a;
a = addr; a = addr;
while (a - addr < 16) while (a - addr < 16)
{ {
if ((a - addr) >= size) if ((uint64_t)(a - addr) >= size)
break ; break ;
else else
ft_printf("%02x", *(unsigned char*)a); ft_printf("%02x", *(unsigned char*)a);
@ -29,17 +29,35 @@ static void print_hex_contents(void *addr, unsigned int size)
} }
} }
void *hexdump(void *addr, unsigned int offset, unsigned int size) void *hexdump(void *addr, uint32_t offset, uint32_t size)
{ {
void *a; void *a;
addr += offset; /* addr += offset; */
a = addr; a = addr;
if (addr == NULL) if (addr == NULL)
return (addr); return (addr);
while ((a - addr) < size) while ((uint32_t)(a - addr) < size)
{ {
ft_printf("%0*llx\t", 16, (a - addr) + (unsigned long)offset); ft_printf("%08llx\t", (a - addr) + offset);
print_hex_contents(a, (uint64_t)(size - (a - addr)));
ft_putchar('\n');
a += 16;
}
return (addr);
}
void *hexdump_64(void *addr, uint64_t offset, uint64_t size)
{
void *a;
/* addr += offset; */
a = addr;
if (addr == NULL)
return (addr);
while ((uint64_t)(a - addr) < size)
{
ft_printf("%016llx\t", (a - addr) + offset);
print_hex_contents(a, (size - (a - addr))); print_hex_contents(a, (size - (a - addr)));
ft_putchar('\n'); ft_putchar('\n');
a += 16; a += 16;

8
nm-otool/otooldiff.sh Executable file
View file

@ -0,0 +1,8 @@
#!/usr/bin/env zsh
make
diff <(./ft_otool $@) <(otool -t $@)
if [ $? -eq 0 ]; then
echo "OK ✅"
else
echo "ERROR ❎"
fi

View file

@ -6,12 +6,12 @@
/* 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/10/26 18:46:22 by jhalford ### ########.fr */ /* Updated: 2017/10/30 12:26:03 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
#define NM_USAGE "usage: nm [-agmnpruU]" #define NM_USAGE "usage: nm [-agmnpruU] filename ..."
t_machodata *g_data = NULL; t_machodata *g_data = NULL;
@ -41,9 +41,9 @@ int nm_file(void *file, t_nmdata *data)
magic = *(int*)file; magic = *(int*)file;
if (IS_MACH_32(magic)) if (IS_MACH_32(magic))
mach_64_dump(file, data); nm_mach(file, data);
else if (IS_MACH_64(magic)) else if (IS_MACH_64(magic))
mach_64_dump(file, data); nm_mach_64(file, data);
else if (IS_FAT(magic)) else if (IS_FAT(magic))
ft_printf("{red}unsupported arch:{eoc} magic=%#x(FAT)\n", magic); ft_printf("{red}unsupported arch:{eoc} magic=%#x(FAT)\n", magic);
else else

View file

@ -6,79 +6,70 @@
/* 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/10/23 16:37:45 by jhalford ### ########.fr */ /* Updated: 2017/10/30 12:26:10 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
#define OTOOL_USAGE "usage: otool [-dt] filename ..."
void *get_text_section(void *file) t_cliopts g_otool_opts[] =
{ {
uint32_t i; {'t', NULL, OTOOL_TEXT, 0, NULL, 0},
uint32_t j; {'d', NULL, OTOOL_DATA, 0, NULL, 0},
struct load_command *lc; };
struct segment_command_64 *seg;
struct section_64 *sect;
lc = (void*)((struct mach_header_64*)file + 1); void otool_file(void *file, t_otooldata *data)
i = -1;
while (++i < ((struct mach_header_64*)file)->ncmds)
{
if (lc->cmd & LC_SEGMENT_64)
{
seg = (struct segment_command_64*)lc;
sect = (void*)(seg + 1);
j = -1;
while (++j < seg->nsects)
{
if (ft_strcmp(sect->sectname, "__text") == 0)
return (sect);
sect = sect + 1;
}
}
lc = (void*)lc + lc->cmdsize;
}
return (NULL);
}
void otool(void *file)
{ {
uint32_t magic; uint32_t magic;
struct section_64 *sect;
magic = *(int *)file; magic = *(int *)file;
if (IS_FAT(magic)) if (IS_MACH_32(magic))
ft_printf("fat binary not supported yet\n"); otool_mach(file, data);
else if (IS_MACH_64(magic)) else if (IS_MACH_64(magic))
{ otool_mach_64(file, data);
sect = get_text_section(file); else if (IS_FAT(magic))
ft_printf("Contents of (__TEXT,__text) section\n"); ft_printf("fat binary not supported yet\n");
hexdump(file, sect->offset, sect->size);
}
else else
ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic); ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic);
} }
int main(int ac, char **av) int otool(int ac, char **av, t_otooldata data)
{ {
int fd; int i;
char *file; struct stat buf;
struct stat buf; int fd;
char *file;
if (ac != 2) i = data.av_data - av;
while (i < ac && av[i])
{ {
ft_dprintf(2, "USAGE PLACEHOLDER\n"); if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1)
return (1); ft_printf("%s:\n", av[i]);
if ((fd = open((av[i]), O_RDONLY)) < 0)
return (1);
if ((fstat(fd, &buf)) < 0)
return (1);
if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
== MAP_FAILED)
return (1);
otool_file(file, &data);
if (munmap(file, buf.st_size))
return (1);
i++;
} }
if ((fd = open(av[1], O_RDONLY)) < 0)
return (1);
if ((fstat(fd, &buf)) < 0)
return (1);
if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
== MAP_FAILED)
return (1);
otool(file);
if (munmap(file, buf.st_size))
return (1);
return (0); return (0);
} }
int main(int ac, char **av)
{
t_otooldata data;
if (cliopts_get(av, g_otool_opts, &data))
{
ft_perror("ft_otool");
ft_dprintf(2, OTOOL_USAGE"\n");
return (1);
}
return (otool(ac, av, data));
}

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_section.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:00:40 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:18:02 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void *get_segment(void *file, char *lookup)
{
uint32_t i;
struct load_command *lc;
struct segment_command *seg;
lc = (void*)((struct mach_header*)file + 1);
i = -1;
while (++i < ((struct mach_header*)file)->ncmds)
{
if (lc->cmd & LC_SEGMENT)
{
seg = (struct segment_command*)lc;
if (ft_strcmp(seg->segname, lookup) == 0)
return (seg);
}
lc = (void*)lc + lc->cmdsize;
}
return (NULL);
}
void *get_section(void *file, char *segname, char *sectname)
{
uint32_t j;
struct segment_command *seg;
struct section *sect;
seg = get_segment(file, segname);
sect = (void*)(seg + 1);
j = -1;
while (++j < seg->nsects)
{
if (ft_strcmp(sect->sectname, sectname) == 0)
return (sect);
sect = sect + 1;
}
return (NULL);
}

71
nm-otool/srcs/mach/mach.c Normal file
View file

@ -0,0 +1,71 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mach.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/23 16:06:44 by jhalford #+# #+# */
/* Updated: 2017/10/30 11:32:11 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
static void symtab_parse(t_machodata *data, struct symtab_command *symtab)
{
int i;
t_symbol symbol;
char *stringtable;
struct nlist *array;
data->symtab = symtab;
stringtable = data->file + symtab->stroff;
array = (struct nlist*)(data->file + symtab->symoff);
i = -1;
while (++i < (int)symtab->nsyms)
{
symbol_init(&symbol, stringtable, array, i);
symbol_set(&symbol, data);
ft_lsteadd(&data->symbols, ft_lstnew(&symbol, sizeof(symbol)));
}
}
static void seg_parse(t_machodata *data, struct segment_command *seg)
{
uint32_t nsects;
uint32_t i;
struct section *sect;
nsects = seg->nsects;
sect = (void*)(seg + 1);
i = -1;
while (++i < nsects)
{
ft_lsteadd(&data->sects, ft_lstnew(&sect, sizeof(sect)));
sect = sect + 1;
}
}
void mach_parse(t_machodata *data)
{
uint32_t ncmds;
uint32_t i;
struct load_command *lc;
struct mach_header *header;
header = data->file;
ncmds = header->ncmds;
lc = (void*)(header + 1);
i = -1;
while (++i < ncmds)
{
if (lc->cmd == LC_SYMTAB)
symtab_parse(data, (struct symtab_command*)lc);
else if (lc->cmd == LC_DYSYMTAB)
data->dysymtab = (struct dysymtab_command*)lc;
else if (lc->cmd == LC_SEGMENT)
seg_parse(data, (struct segment_command*)lc);
lc = (void*)lc + lc->cmdsize;
}
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nm_mach.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */
/* Updated: 2017/10/30 11:31:38 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void nm_mach(struct mach_header *file, t_nmdata *data)
{
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);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* otool_mach.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:04:06 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:16:36 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void otool_mach(void *file, t_otooldata *data)
{
struct section *sect;
if (data->flag & OTOOL_TEXT)
{
if ((sect = get_section(file, SEG_TEXT, SECT_TEXT)))
{
ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT);
hexdump(file + sect->offset, sect->addr, sect->size);
}
}
if (data->flag & OTOOL_DATA)
{
if ((sect = get_section(file, SEG_DATA, SECT_DATA)))
{
ft_printf("Contents of (%s,%s) section\n", SEG_DATA, SECT_DATA);
hexdump(file + sect->offset, sect->addr, sect->size);
}
}
}

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_filter.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:44:51 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
int cmp_symtype(t_symbol *sym, void *type)
{
return ((1 << sym->type) & *(uint64_t*)type);
}
inline int is_not_external(t_symbol *s)
{
return (!(s->nlist->n_type & N_EXT));
}
inline int is_external(t_symbol *s)
{
return (s->nlist->n_type & N_EXT);
}
int symbol_filter(t_list **symbols, t_flag flag)
{
uint64_t allowed_syms;
allowed_syms = ~(1 << SYM_STAB);
if (flag & NM_ALL)
allowed_syms |= (1 << SYM_STAB);
if (flag & NM_NO_UNDF)
allowed_syms &= ~(1 << SYM_UNDF);
if (flag & NM_ONLY_UNDF)
allowed_syms &= (1 << SYM_UNDF);
ft_lst_filterout(symbols, &allowed_syms, cmp_symtype, symbol_free);
if (flag & NM_NO_LOCAL)
ft_lst_filterout(symbols, NULL, is_external, symbol_free);
return (0);
}

View file

@ -0,0 +1,88 @@
#include "ft_nm_otool.h"
t_symbolmap g_symbolmap[] =
{
{'U', "undefined"},
{'A', "absolute"},
{'T', "segname missing"},
{'D', "segname missing"},
{'B', "segname missing"},
{'C', "common"},
{'-', "?"},
{'S', "segname missing"},
{'I', "indirect"},
};
void symbol_format_desc(t_symbol *symbol)
{
if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY)
ft_printf(" [referenced dynamically]");
if (is_external(symbol))
ft_printf(" external");
else
{
ft_printf(" non-external");
if (symbol->nlist->n_type & N_PEXT)
ft_printf(" (was a private external)");
}
}
void symbol_format_m(t_symbol *symbol)
{
t_symbolmap map;
map = g_symbolmap[symbol->type];
ft_printf(" (%s%c%s)",
symbol->section ? symbol->section->segname : map.s,
symbol->section ? ',' : 0,
symbol->section ? symbol->section->sectname : "");
symbol_format_desc(symbol);
}
void symbol_format_full(t_symbol *symbol)
{
ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x",
symbol->pos,
(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);
}
void symbol_format_dfl(t_symbol *symbol)
{
t_symbolmap map;
map = g_symbolmap[symbol->type];
if (is_external(symbol))
ft_printf(" %c", map.c);
else
ft_printf(" %c", map.c + 'a' - 'A');
if (symbol->type == SYM_STAB)
ft_printf(" %02x %04b %#x",
symbol->nlist->n_sect, symbol->nlist->n_desc,
symbol->nlist->n_type);
}
int symbol_format(t_symbol *symbol, t_nmdata *data)
{
if (data->flag & NM_OFORMAT)
ft_printf("%s: ", data->filename);
if (!(data->flag & NM_ONLY_UNDF))
{
if (symbol->type == SYM_UNDF)
ft_printf("%8s", " ");
else
ft_printf("%08llx", symbol->nlist->n_value);
if (data->flag & NM_MFORMAT)
symbol_format_m(symbol);
else if (data->flag & NM_FULL)
symbol_format_full(symbol);
else
symbol_format_dfl(symbol);
}
ft_printf(" %s\n", symbol->string);
return (0);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/26 17:06:23 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:38:31 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void symbol_free(void *data, size_t size)
{
t_symbol *symbol;
symbol = data;
(void)size;
if (symbol)
free(symbol);
}

View file

@ -0,0 +1,66 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:23:24 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
t_machodata *g_data;
int symbol_init(t_symbol *symbol,
char *stringtable, struct nlist *array, int i)
{
symbol->type = 0;
symbol->pos = i;
symbol->nlist = array + i;
symbol->string = stringtable + array[i].n_un.n_strx;
return (0);
}
static int symbol_gettype(int type_mask,
struct nlist *nlist, struct section *section)
{
if (nlist->n_type & N_STAB)
return (SYM_STAB);
else if (type_mask == N_UNDF && nlist->n_type & N_EXT && nlist->n_value)
return (SYM_COMMON);
else if (type_mask == N_UNDF)
return (SYM_UNDF);
else if (type_mask == N_ABS)
return (SYM_ABS);
else if (type_mask == N_SECT && ft_strequ("__text", section->sectname))
return (SYM_TEXT);
else if (type_mask == N_SECT && ft_strequ("__data", section->sectname))
return (SYM_DATA);
else if (type_mask == N_SECT && ft_strequ("__bss", section->sectname))
return (SYM_BSS);
else if (type_mask == N_INDR)
return (SYM_INDR);
else
return (SYM_OTHER);
}
int symbol_set(t_symbol *sym, t_machodata *data)
{
t_list *lst;
if (sym->nlist->n_sect)
{
lst = ft_lst_at(data->sects, sym->nlist->n_sect - 1);
sym->section = *(struct section**)(lst->content);
}
else
sym->section = NULL;
sym->type = symbol_gettype(
sym->nlist->n_type & N_TYPE,
sym->nlist,
sym->section);
return (0);
}

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_sort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:46:04 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
static int sym_asort(t_symbol *sym1, t_symbol *sym2)
{
return (ft_strcmp(sym1->string, sym2->string));
}
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);
}
int symbol_sort(t_list **syms, t_flag flag)
{
if (!(flag & NM_NOSORT))
{
if (flag & NM_ASORT)
ft_lstsort(syms, sym_asort);
else if (flag & NM_NSORT)
ft_lstsort(syms, sym_nsort);
if (flag & NM_RSORT)
ft_lst_reverse(syms);
}
return (0);
}

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* get_section_64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:00:40 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:18:02 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void *get_segment_64(void *file, char *lookup)
{
uint32_t i;
struct load_command *lc;
struct segment_command_64 *seg;
lc = (void*)((struct mach_header_64*)file + 1);
i = -1;
while (++i < ((struct mach_header_64*)file)->ncmds)
{
if (lc->cmd & LC_SEGMENT_64)
{
seg = (struct segment_command_64*)lc;
if (ft_strcmp(seg->segname, lookup) == 0)
return (seg);
}
lc = (void*)lc + lc->cmdsize;
}
return (NULL);
}
void *get_section_64(void *file, char *segname, char *sectname)
{
uint32_t j;
struct segment_command_64 *seg;
struct section_64 *sect;
seg = get_segment_64(file, segname);
sect = (void*)(seg + 1);
j = -1;
while (++j < seg->nsects)
{
if (ft_strcmp(sect->sectname, sectname) == 0)
return (sect);
sect = sect + 1;
}
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/10/26 18:45:25 by jhalford ### ########.fr */ /* Updated: 2017/10/30 11:32:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -69,16 +69,3 @@ void mach_64_parse(t_machodata *data)
lc = (void*)lc + lc->cmdsize; lc = (void*)lc + lc->cmdsize;
} }
} }
void mach_64_dump(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

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nm_mach_64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */
/* Updated: 2017/10/30 11:35:53 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

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* otool_mach_64.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:04:06 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:16:36 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void otool_mach_64(void *file, t_otooldata *data)
{
struct section_64 *sect;
if (data->flag & OTOOL_TEXT)
{
if ((sect = get_section_64(file, SEG_TEXT, SECT_TEXT)))
{
ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT);
hexdump_64(file + sect->offset, sect->addr, sect->size);
}
}
if (data->flag & OTOOL_DATA)
{
if ((sect = get_section_64(file, SEG_DATA, SECT_DATA)))
{
ft_printf("Contents of (%s,%s) section\n", SEG_DATA, SECT_DATA);
hexdump_64(file + sect->offset, sect->addr, sect->size);
}
}
}

View file

@ -1,6 +1,6 @@
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
t_symbolmap g_symbolmap[] = t_symbolmap g_symbolmap_64[] =
{ {
{'U', "undefined"}, {'U', "undefined"},
{'A', "absolute"}, {'A', "absolute"},
@ -31,7 +31,7 @@ void symbol_64_format_m(t_symbol_64 *symbol)
{ {
t_symbolmap map; t_symbolmap map;
map = g_symbolmap[symbol->type]; map = g_symbolmap_64[symbol->type];
ft_printf(" (%s%c%s)", ft_printf(" (%s%c%s)",
symbol->section ? symbol->section->segname : map.s, symbol->section ? symbol->section->segname : map.s,
symbol->section ? ',' : 0, symbol->section ? ',' : 0,
@ -55,7 +55,7 @@ void symbol_64_format_dfl(t_symbol_64 *symbol)
{ {
t_symbolmap map; t_symbolmap map;
map = g_symbolmap[symbol->type]; map = g_symbolmap_64[symbol->type];
if (is_external_64(symbol)) if (is_external_64(symbol))
ft_printf(" %c", map.c); ft_printf(" %c", map.c);
else else