This commit is contained in:
Jack Halford 2017-11-01 13:00:12 +01:00
parent aecf9be263
commit 519b50e304
34 changed files with 386 additions and 235 deletions

View file

@ -34,8 +34,6 @@ 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 = \
bswap.c\
dump_symtab.c\
endianness.c\ endianness.c\
fat.c\ fat.c\
ft_nm.c\ ft_nm.c\
@ -57,7 +55,8 @@ 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\
ppc/otool_ppc.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))

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/10/30 12:00:49 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:19:05 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -92,4 +92,6 @@ struct s_symbolmap
char *s; char *s;
}; };
void nm_single_file(void *file, t_nmdata *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/10/31 16:03:56 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:20:22 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -31,19 +31,18 @@
# include "mach_64.h" # include "mach_64.h"
# include "mach.h" # include "mach.h"
# include "ppc.h"
# define IS_MACH_64(x) (*(uint32_t*)x == MH_MAGIC_64 || *(uint32_t*)x == MH_CIGAM_64) typedef unsigned int t_u;
# define IS_MACH_32(x) (*(uint32_t*)x == MH_MAGIC || *(uint32_t*)x == MH_CIGAM)
# define IS_FAT(x) (*(uint32_t*)x == FAT_MAGIC || *(uint32_t*)x == FAT_CIGAM)
# define IS_REV(x) (*(uint32_t*)x == MH_CIGAM || *(uint32_t*)x == MH_CIGAM_64 || *(uint32_t*)x == FAT_CIGAM)
uint64_t endian(uint64_t n, uint8_t size); # define IS_MACH_64(x) (*(t_u*)x == MH_MAGIC_64||*(t_u*)x == MH_CIGAM_64)
uint8_t bswap_8(uint8_t x); # define IS_MACH_32(x) (*(t_u*)x == MH_MAGIC||*(t_u*)x == MH_CIGAM)
uint16_t bswap_16(uint16_t x); # define IS_FAT(x) (*(t_u*)x == FAT_MAGIC||*(t_u*)x == FAT_CIGAM)
uint32_t bswap_32(uint32_t x); # define IS_REV(x) ({t_u m=*(t_u*)x;m==MH_CIGAM||m==MH_CIGAM_64||m==FAT_CIGAM;})
uint64_t bswap_64(uint64_t x);
void *fat_extract(struct fat_header *fat, uint64_t endian(uint64_t n, uint8_t size);
void *fat_extract(struct fat_header *fat,
cpu_type_t cputype, cpu_type_t cputype,
cpu_subtype_t cpusubtype); cpu_subtype_t cpusubtype);

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/10/31 19:15:26 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:19:16 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -30,4 +30,6 @@ struct s_otooldata
char *filename; char *filename;
}; };
void otool_single_file(void *file, t_otooldata *data);
#endif #endif

View file

@ -1,44 +1,44 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* mach.h :+: :+: :+: */ /* mach.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */ /* Created: 2017/11/01 12:03:56 by jhalford #+# #+# */
/* Updated: 2017/10/31 16:30:06 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:08:34 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MACH_H #ifndef MACH_H
# define MACH_H # define MACH_H
typedef struct s_symbol t_symbol; typedef struct s_symbol t_symbol;
struct s_symbol struct s_symbol
{ {
int pos; int pos;
t_symtype type; t_symtype type;
char *string; char *string;
struct nlist nlist; struct nlist nlist;
struct section *section; struct section *section;
}; };
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);
int symbol_set(t_symbol *symbol, t_machodata *data); int symbol_set(t_symbol *symbol, t_machodata *data);
void symbol_free(void *data, size_t size); void symbol_free(void *data, size_t size);
int symbol_sort(t_list **syms, t_flag flag); 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_nmdata *data);
void *get_section(void *file, char *segname, void *get_section(struct mach_header *file,
char *sectname); char *lookup);
void mach_parse(t_machodata *data); void mach_parse(t_machodata *data);
void nm_mach(struct mach_header *file, void nm_mach(struct mach_header *file,
t_nmdata *data); t_nmdata *data);
void otool_mach(void *file, t_otooldata *data); void otool_mach(void *file, t_otooldata *data);
#endif #endif

View file

@ -6,39 +6,39 @@
/* 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/31 16:30:06 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:08:51 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef MACH_64_H #ifndef MACH_64_H
# define MACH_64_H # define MACH_64_H
typedef struct s_symbol_64 t_symbol_64; typedef struct s_symbol_64 t_symbol_64;
struct s_symbol_64 struct s_symbol_64
{ {
int pos; int pos;
t_symtype type; t_symtype type;
char *string; char *string;
struct nlist_64 nlist; struct nlist_64 nlist;
struct section_64 *section; struct section_64 *section;
}; };
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);
int symbol_64_set(t_symbol_64 *symbol, t_machodata *data); int symbol_64_set(t_symbol_64 *symbol, t_machodata *data);
void symbol_64_free(void *data, size_t size); void symbol_64_free(void *data, size_t size);
int symbol_64_sort(t_list **syms, t_flag flag); 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_nmdata *data);
void *get_section_64(void *file, char *segname, void *get_section_64(struct mach_header_64 *file,
char *sectname); 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(struct mach_header_64 *file,
t_nmdata *data); t_nmdata *data);
void otool_mach_64(void *file, t_otooldata *data); void otool_mach_64(void *file, t_otooldata *data);
#endif #endif

18
nm-otool/includes/ppc.h Normal file
View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ppc.h :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 11:38:10 by jhalford #+# #+# */
/* Updated: 2017/11/01 11:38:29 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#ifndef PPC_H
# define PPC_H
void otool_ppc(void *file, t_otooldata *data);
#endif

View file

@ -108,6 +108,7 @@ lst/lst_insert_sort.c\
lst/pop.c\ lst/pop.c\
lst/push.c\ lst/push.c\
lst/top.c\ lst/top.c\
math/bswap.c\
math/ft_addrcmp.c\ math/ft_addrcmp.c\
math/ft_ilen.c\ math/ft_ilen.c\
math/ft_ilen_base.c\ math/ft_ilen_base.c\
@ -144,6 +145,7 @@ printing/ft_putendl.c\
printing/ft_putnbr.c\ printing/ft_putnbr.c\
printing/ft_putstr.c\ printing/ft_putstr.c\
printing/hexdump.c\ printing/hexdump.c\
printing/hexdump_int.c\
rs/rs.c\ rs/rs.c\
sstr/ft_sstradd.c\ sstr/ft_sstradd.c\
sstr/ft_sstrcat.c\ sstr/ft_sstrcat.c\

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/30 11:25:37 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:58:06 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -89,8 +89,11 @@ 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, uint32_t offset, uint32_t size); void *hexdump(void *addr, uint32_t offset, uint32_t size, int swap);
void *hexdump_64(void *addr, uint64_t offset, uint64_t size); void *hexdump_64(void *addr, uint64_t offset, uint64_t size, int swap);
void *hexdump_int(void *addr, uint32_t offset, uint32_t size, int swap);
void *hexdump_64_int(void *addr, uint64_t offset, uint64_t size, int swap);
void *ft_realloc(void *data, int size); void *ft_realloc(void *data, int size);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/20 15:41:59 by jhalford #+# #+# */ /* Created: 2017/03/20 15:41:59 by jhalford #+# #+# */
/* Updated: 2017/10/30 15:43:39 by jhalford ### ########.fr */ /* Updated: 2017/11/01 11:53:22 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,18 +21,23 @@
# define FT_MAX(a, b) ((a) > (b) ? (a) : (b)) # define FT_MAX(a, b) ((a) > (b) ? (a) : (b))
# define FT_DIST(a, b) FT_ABS((a) - (b)) # define FT_DIST(a, b) FT_ABS((a) - (b))
char *ft_itoa(int n); char *ft_itoa(int n);
char *ft_itoa_base(int nbr, char *base, char *flags); char *ft_itoa_base(int nbr, char *base, char *flags);
char *ft_lltoa_base(long long nbr, char *base, char *flags); char *ft_lltoa_base(long long nbr, char *base, char *flags);
char *ft_ulltoa_base(unsigned long long nbr, char *base); char *ft_ulltoa_base(unsigned long long nbr, char *base);
char *ft_uitoa_base(unsigned int nbr, char *base); char *ft_uitoa_base(unsigned int nbr, char *base);
size_t ft_ilen(int n); size_t ft_ilen(int n);
size_t ft_ilen_base(int n, int base); size_t ft_ilen_base(int n, int base);
size_t ft_uilen(unsigned int n); size_t ft_uilen(unsigned int n);
size_t ft_lllen(long long n); size_t ft_lllen(long long n);
size_t ft_lllen_base(long long n, int base); size_t ft_lllen_base(long long n, int base);
int ft_addrcmp(void *a, void *b); int ft_addrcmp(void *a, void *b);
void *id(void *data); void *id(void *data);
uint8_t bswap_8(uint8_t x);
uint16_t bswap_16(uint16_t x);
uint32_t bswap_32(uint32_t x);
uint64_t bswap_64(uint64_t x);
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */ /* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/04 11:09:58 by jhalford #+# #+# */ /* Created: 2016/11/04 11:09:58 by jhalford #+# #+# */
/* Updated: 2017/10/31 19:05:46 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:41:49 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,7 +21,6 @@ void ft_lstsort(t_list **begin_list, int (*cmp)())
indirect = begin_list; indirect = begin_list;
if (!*begin_list) if (!*begin_list)
return ; return ;
int i = 0;
while (*indirect && (*indirect)->next) while (*indirect && (*indirect)->next)
{ {
if ((*cmp)((*indirect)->content, (*indirect)->next->content) > 0) if ((*cmp)((*indirect)->content, (*indirect)->next->content) > 0)
@ -32,7 +31,6 @@ void ft_lstsort(t_list **begin_list, int (*cmp)())
*indirect = tmp2; *indirect = tmp2;
(*indirect)->next = tmp; (*indirect)->next = tmp;
indirect = begin_list; indirect = begin_list;
i = 0;
} }
else else
indirect = &(*indirect)->next; indirect = &(*indirect)->next;

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 15:20:32 by jhalford #+# #+# */ /* Created: 2017/10/30 15:20:32 by jhalford #+# #+# */
/* Updated: 2017/10/30 15:53:14 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:38:00 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "libft.h"
inline uint8_t bswap_8(uint8_t x) inline uint8_t bswap_8(uint8_t x)
{ {
@ -19,8 +19,7 @@ inline uint8_t bswap_8(uint8_t x)
inline uint16_t bswap_16(uint16_t x) inline uint16_t bswap_16(uint16_t x)
{ {
return (( (x >> 8) & 0xffu) | ( return (((x >> 8) & 0xffu) | ((x & 0xffu) << 8));
(x & 0xffu) << 8));
} }
inline uint32_t bswap_32(uint32_t x) inline uint32_t bswap_32(uint32_t x)

View file

@ -6,14 +6,14 @@
/* 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/30 11:25:51 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:57:09 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include <stdio.h> #include <stdio.h>
#include "libft.h" #include "libft.h"
static void print_hex_contents(void *addr, uint64_t size) static void print_hex_contents(void *addr, uint64_t size, int swap)
{ {
void *a; void *a;
@ -22,43 +22,43 @@ static void print_hex_contents(void *addr, uint64_t size)
{ {
if ((uint64_t)(a - addr) >= size) if ((uint64_t)(a - addr) >= size)
break ; break ;
else if (swap)
ft_printf("%02x", bswap_8(*(uint8_t*)a));
else else
ft_printf("%02x", *(unsigned char*)a); ft_printf("%02x", *(uint8_t*)a);
ft_putchar(' '); ft_putchar(' ');
a++; a++;
} }
} }
void *hexdump(void *addr, uint32_t offset, uint32_t size) void *hexdump(void *addr, uint32_t offset, uint32_t size, int swap)
{ {
void *a; void *a;
/* addr += offset; */
a = addr; a = addr;
if (addr == NULL) if (addr == NULL)
return (addr); return (addr);
while ((uint32_t)(a - addr) < size) while ((uint32_t)(a - addr) < size)
{ {
ft_printf("%08llx\t", (a - addr) + offset); ft_printf("%08llx\t", (a - addr) + offset);
print_hex_contents(a, (uint64_t)(size - (a - addr))); print_hex_contents(a, (size - (a - addr)), swap);
ft_putchar('\n'); ft_putchar('\n');
a += 16; a += 16;
} }
return (addr); return (addr);
} }
void *hexdump_64(void *addr, uint64_t offset, uint64_t size) void *hexdump_64(void *addr, uint64_t offset, uint64_t size, int swap)
{ {
void *a; void *a;
/* addr += offset; */
a = addr; a = addr;
if (addr == NULL) if (addr == NULL)
return (addr); return (addr);
while ((uint64_t)(a - addr) < size) while ((uint64_t)(a - addr) < size)
{ {
ft_printf("%016llx\t", (a - addr) + offset); ft_printf("%016llx\t", (a - addr) + offset);
print_hex_contents(a, (size - (a - addr))); print_hex_contents(a, (size - (a - addr)), swap);
ft_putchar('\n'); ft_putchar('\n');
a += 16; a += 16;
} }

View file

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* hexdump_int.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 12:57:21 by jhalford #+# #+# */
/* Updated: 2017/11/01 12:59:57 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include "libft.h"
static void print_hex_contents(void *addr, uint64_t size, int swap)
{
void *a;
a = addr;
while (a - addr < 16)
{
if ((uint64_t)(a - addr) >= size)
break ;
else if (swap)
ft_printf("%08x", bswap_32(*(uint64_t*)a));
else
ft_printf("%08x", *(uint64_t*)a);
ft_putchar(' ');
a += 4;
}
}
void *hexdump_int(void *addr, uint32_t offset, uint32_t size, int swap)
{
void *a;
a = addr;
if (addr == NULL)
return (addr);
while ((uint32_t)(a - addr) < size)
{
ft_printf("%08llx\t", (a - addr) + offset);
print_hex_contents(a, (size - (a - addr)), swap);
ft_putchar('\n');
a += 16;
}
return (addr);
}
void *hexdump_64_int(void *addr, uint64_t offset, uint64_t size,
int swap)
{
void *a;
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)), swap);
ft_putchar('\n');
a += 16;
}
return (addr);
}

View file

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

View file

@ -1,7 +1,7 @@
#!/usr/bin/env zsh #!/usr/bin/env zsh
diff <(./ft_nm $@) <(nm $@) diff <(./ft_nm $@) <(nm $@)
if [ $? -eq 0 ]; then if [ $? -eq 0 ]; then
echo "nm $@\t OK ✅ " echo "OK ✅ nm $@"
else else
echo "nm $@\t ERROR ✅ " echo "ERROR ✅ nm $@"
fi fi

View file

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

View file

@ -1,34 +0,0 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* dump_symtab.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/25 19:39:15 by jhalford #+# #+# */
/* Updated: 2017/10/23 16:49:46 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
void dump_dysymtab(t_machodata *data, struct dysymtab_command *dysymtab)
{
(void)data;
ft_printf("{blu}{inv}struct dysymtab_command{eoc}\n");
ft_printf("ilocalsym %i\n", dysymtab->ilocalsym);
ft_printf("nlocalsym %i\n", dysymtab->nlocalsym);
ft_printf("iextdefsym %i\n", dysymtab->iextdefsym);
ft_printf("nextdefsym %i\n", dysymtab->nextdefsym);
ft_printf("iundefsym %i\n", dysymtab->iundefsym);
ft_printf("nundefsym %i\n", dysymtab->nundefsym);
ft_printf("---------------\n");
ft_printf("ntoc %i\n", dysymtab->ntoc);
ft_printf("nmodtab %i\n", dysymtab->nmodtab);
ft_printf("nextrefsyms %i\n", dysymtab->nextrefsyms);
ft_printf("nmodtab %i\n", dysymtab->nmodtab);
ft_printf("nindirectsims %i\n", dysymtab->nindirectsyms);
ft_printf("nextrel %i\n", dysymtab->nextrel);
ft_printf("nlocrel %i\n", dysymtab->nlocrel);
ft_putendl("");
}

View file

@ -6,7 +6,7 @@
/* 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/10/31 17:45:48 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:23:08 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,7 +16,7 @@ void *fat_extract(struct fat_header *fat,
cpu_type_t cputype, cpu_type_t cputype,
cpu_subtype_t cpusubtype) cpu_subtype_t cpusubtype)
{ {
uint32_t narch; uint32_t narch;
struct fat_arch *arch; struct fat_arch *arch;
narch = endian(fat->nfat_arch, 32); narch = endian(fat->nfat_arch, 32);

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/10/31 19:45:02 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:52:01 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -33,16 +33,14 @@ t_cliopts g_nm_opts[] =
{0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0},
}; };
void nm_single_file(void *file, t_nmdata *data);
void nm_fat_file(struct fat_header *fat, t_nmdata *data) void nm_fat_file(struct fat_header *fat, t_nmdata *data)
{ {
uint32_t narch; uint32_t narch;
struct fat_arch *obj; struct fat_arch *obj;
const NXArchInfo *arch;
g_rev = IS_REV(fat); g_rev = IS_REV(fat);
obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL); if ((obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL)))
if (obj)
nm_single_file(obj, data); nm_single_file(obj, data);
else else
{ {
@ -50,10 +48,11 @@ void nm_fat_file(struct fat_header *fat, t_nmdata *data)
obj = (struct fat_arch*)(fat + 1); obj = (struct fat_arch*)(fat + 1);
while (narch--) while (narch--)
{ {
const NXArchInfo *arch = NXGetArchInfoFromCpuType( arch = NXGetArchInfoFromCpuType(
endian(obj->cputype, 32), endian(obj->cputype, 32),
endian(obj->cpusubtype, 32)); endian(obj->cpusubtype, 32));
ft_printf("\n%s (for architecture %s):\n", data->filename, arch->name); ft_printf("\n%s (for architecture %s):\n",
data->filename, arch->name);
nm_single_file((void*)fat + endian(obj->offset, 32), data); nm_single_file((void*)fat + endian(obj->offset, 32), data);
g_rev = IS_REV(fat); g_rev = IS_REV(fat);
++obj; ++obj;
@ -63,6 +62,8 @@ 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_nmdata *data)
{ {
const NXArchInfo *arch;
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(file, data);
@ -70,13 +71,13 @@ void nm_single_file(void *file, t_nmdata *data)
nm_mach_64(file, data); nm_mach_64(file, data);
else else
{ {
const NXArchInfo *arch = NXGetArchInfoFromCpuType( arch = NXGetArchInfoFromCpuType(
endian(*((int32_t*)file + 1), 32), endian(*((int32_t*)file + 1), 32),
endian(*((int32_t*)file + 2), 32)); endian(*((int32_t*)file + 2), 32));
if (arch) if (arch)
ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name); ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name);
else else
ft_dprintf(2, "unknown architecture, magic=%#x\n", *((int32_t*)file)); ft_dprintf(2, "unknown arch: magic=%#x\n", *((int32_t*)file));
} }
} }
@ -88,13 +89,9 @@ int nm(int ac, char **av, t_nmdata data)
void *file; void *file;
i = data.av_data - av; i = data.av_data - av;
while (i < ac && av[i]) while (i < ac && (data.filename = av[i]))
{ {
g_rev = 0; if ((fd = open(data.filename, O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
data.filename = av[i];
if ((fd = open((data.filename), O_RDONLY)) < 0)
return (1);
if ((fstat(fd, &buf)) < 0)
return (1); return (1);
if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
== MAP_FAILED) == MAP_FAILED)

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/10/31 19:45:03 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:33:22 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,29 +20,30 @@ t_cliopts g_otool_opts[] =
}; };
int g_rev = 0; int g_rev = 0;
void otool_single_file(void *file, t_otooldata *data);
void otool_fat_file(struct fat_header *fat, t_otooldata *data) void otool_fat_file(struct fat_header *fat, t_otooldata *data)
{ {
uint32_t narch; uint32_t narch;
struct fat_arch *obj; struct fat_arch *obj;
const NXArchInfo *arch;
g_rev = IS_REV(fat); g_rev = IS_REV(fat);
obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL); if ((obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL)))
if (obj) {
ft_printf("%s:\n", data->filename);
otool_single_file(obj, data); otool_single_file(obj, data);
}
else else
{ {
narch = endian(fat->nfat_arch, 32); narch = endian(fat->nfat_arch, 32);
obj = (struct fat_arch*)(fat + 1); obj = (struct fat_arch*)(fat + 1);
while (narch--) while (narch--)
{ {
const NXArchInfo *arch = NXGetArchInfoFromCpuType( g_rev = IS_REV(fat);
arch = NXGetArchInfoFromCpuType(
endian(obj->cputype, 32), endian(obj->cputype, 32),
endian(obj->cpusubtype, 32)); endian(obj->cpusubtype, 32));
ft_printf("\n%s (for architecture %s):\n", data->filename, arch->name); ft_printf("%s (architecture %s):\n", data->filename, arch->name);
otool_single_file((void*)fat + endian(obj->offset, 32), data); otool_single_file((void*)fat + endian(obj->offset, 32), data);
g_rev = IS_REV(fat);
++obj; ++obj;
} }
} }
@ -50,21 +51,25 @@ 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_otooldata *data)
{ {
const NXArchInfo *arch;
g_rev = IS_REV(file); g_rev = IS_REV(file);
if (IS_MACH_32(file)) arch = NXGetArchInfoFromCpuType(
otool_mach(file, data); endian(*((int32_t*)file + 1), 32),
else if (IS_MACH_64(file)) endian(*((int32_t*)file + 2), 32));
otool_mach_64(file, data); if (arch)
else
{ {
const NXArchInfo *arch = NXGetArchInfoFromCpuType( if (ft_strcmp(arch->name, "ppc") == 0)
endian(*((int32_t*)file + 1), 32), otool_ppc(file, data);
endian(*((int32_t*)file + 2), 32)); else if (IS_MACH_32(file))
if (arch) otool_mach(file, data);
ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name); else if (IS_MACH_64(file))
otool_mach_64(file, data);
else else
ft_dprintf(2, "unknown architecture, magic=%#x\n", *((int32_t*)file)); ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name);
} }
else
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_otooldata data)
@ -75,12 +80,9 @@ int otool(int ac, char **av, t_otooldata data)
void *file; void *file;
i = data.av_data - av; i = data.av_data - av;
while (i < ac && av[i]) while (i < ac && (data.filename = av[i]))
{ {
ft_printf("%s:\n", av[i]); if ((fd = open((av[i]), O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
if ((fd = open((av[i]), O_RDONLY)) < 0)
return (1);
if ((fstat(fd, &buf)) < 0)
return (1); return (1);
if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0)) if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
== MAP_FAILED) == MAP_FAILED)
@ -88,7 +90,10 @@ int otool(int ac, char **av, t_otooldata data)
if (IS_FAT(file)) if (IS_FAT(file))
otool_fat_file(file, &data); otool_fat_file(file, &data);
else else
{
ft_printf("%s:\n", data.filename);
otool_single_file(file, &data); otool_single_file(file, &data);
}
if (munmap(file, buf.st_size)) if (munmap(file, buf.st_size))
return (1); return (1);
i++; i++;

View file

@ -1,52 +1,68 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* get_section.c :+: :+: :+: */ /* get_section.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* 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/10/30 12:18:02 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:11:10 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
void *get_segment(void *file, char *lookup) void *get_segment(struct mach_header *file, 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;
lc = (void*)((struct mach_header*)file + 1); lc = (void*)(file + 1);
i = -1; i = -1;
while (++i < ((struct mach_header*)file)->ncmds) ncmds = endian(file->ncmds, 32);
DG("ncmds=%i", ncmds);
while (++i < ncmds)
{ {
if (lc->cmd & LC_SEGMENT) DG("i=%i", i);
if (endian(lc->cmd, 32) & LC_SEGMENT)
{ {
seg = (struct segment_command*)lc; 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 + lc->cmdsize; lc = (void*)lc + endian(lc->cmdsize, 32);
} }
return (NULL); return (NULL);
} }
void *get_section(void *file, char *segname, char *sectname) void *get_section(struct mach_header *file, char *lookup)
{ {
uint32_t i;
uint32_t j; uint32_t j;
struct segment_command *seg; struct load_command *lc;
struct section *sect; struct segment_command *seg;
struct section *sect;
seg = get_segment(file, segname); lc = (void*)(file + 1);
sect = (void*)(seg + 1); i = -1;
j = -1; while (++i < endian(file->ncmds, 32))
while (++j < seg->nsects)
{ {
if (ft_strcmp(sect->sectname, sectname) == 0) if (endian(lc->cmd, 32) & LC_SEGMENT)
return (sect); {
sect = sect + 1; seg = (struct segment_command*)lc;
sect = (void*)(seg + 1);
j = -1;
while (++j < endian(seg->nsects, 32))
{
if (ft_strcmp(sect->sectname, lookup) == 0)
return (sect);
sect = sect + 1;
}
}
lc = (void*)lc + endian(lc->cmdsize, 32);
} }
return (NULL); return (NULL);
} }

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* mach.c :+: :+: :+: */ /* mach.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* 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/31 17:46:14 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:09:52 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -52,7 +52,7 @@ void mach_parse(t_machodata *data)
uint32_t ncmds; uint32_t ncmds;
uint32_t i; uint32_t i;
struct load_command *lc; struct load_command *lc;
struct mach_header *header; struct mach_header *header;
header = data->file; header = data->file;
ncmds = endian(header->ncmds, 32); ncmds = endian(header->ncmds, 32);

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* nm_mach.c :+: :+: :+: */ /* nm_mach.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */ /* Created: 2017/10/30 11:03:04 by jhalford #+# #+# */
/* Updated: 2017/10/30 11:31:38 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:10:08 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,35 +1,39 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* otool_mach.c :+: :+: :+: */ /* otool_mach.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* 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/10/30 12:16:36 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:58:39 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
extern int g_rev;
void otool_mach(void *file, t_otooldata *data) void otool_mach(void *file, t_otooldata *data)
{ {
struct section *sect; struct section *sect;
if (data->flag & OTOOL_TEXT) if (data->flag & OTOOL_TEXT)
{ {
if ((sect = get_section(file, SEG_TEXT, SECT_TEXT))) if ((sect = get_section(file, 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 + sect->offset, sect->addr, sect->size); hexdump(file + endian(sect->offset, 32),
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, SEG_DATA, SECT_DATA))) if ((sect = get_section(file, 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 + sect->offset, sect->addr, sect->size); hexdump(file + endian(sect->offset, 32),
endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
} }
} }

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* symbol_filter.c :+: :+: :+: */ /* symbol_filter.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* 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/10/31 16:36:23 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:11:33 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 12:33:27 by jhalford #+# #+# */
/* Updated: 2017/11/01 12:33:27 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
t_symbolmap g_symbolmap[] = t_symbolmap g_symbolmap[] =

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/10/26 18:38:31 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:11:42 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* symbol_init.c :+: :+: :+: */ /* symbol_init.c :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* 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 16:42:20 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:36:06 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* 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/10/31 19:58:07 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:37:02 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -38,21 +38,31 @@ void *get_segment_64(struct mach_header_64 *file, char *lookup)
return (NULL); return (NULL);
} }
void *get_section_64(void *file, char *segname, char *sectname) void *get_section_64(struct mach_header_64 *file, char *lookup)
{ {
uint32_t i;
uint32_t j; uint32_t j;
struct load_command *lc;
struct segment_command_64 *seg; struct segment_command_64 *seg;
struct section_64 *sect; struct section_64 *sect;
if (!(seg = get_segment_64(file, segname))) lc = (void*)(file + 1);
return (NULL); i = -1;
sect = (void*)(seg + 1); while (++i < endian(file->ncmds, 32))
j = -1;
while (++j < endian(seg->nsects, 32))
{ {
if (ft_strcmp(sect->sectname, sectname) == 0) if (endian(lc->cmd, 32) & LC_SEGMENT_64)
return (sect); {
sect = sect + 1; seg = (struct segment_command_64*)lc;
sect = (void*)(seg + 1);
j = -1;
while (++j < endian(seg->nsects, 32))
{
if (ft_strcmp(sect->sectname, lookup) == 0)
return (sect);
sect = sect + 1;
}
}
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/10/31 18:06:12 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:36:21 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,7 +23,7 @@ static void symtab_64_parse(t_machodata *data, struct symtab_command *symtab)
stringtable = data->file + endian(symtab->stroff, 32); stringtable = data->file + endian(symtab->stroff, 32);
array = (struct nlist_64*)(data->file + endian(symtab->symoff, 32)); array = (struct nlist_64*)(data->file + endian(symtab->symoff, 32));
i = -1; i = -1;
while (++i < (int)endian(symtab->nsyms,32)) while (++i < (int)endian(symtab->nsyms, 32))
{ {
symbol_64_init(&symbol, stringtable, array, i); symbol_64_init(&symbol, stringtable, array, i);
symbol_64_set(&symbol, data); symbol_64_set(&symbol, data);

View file

@ -6,38 +6,34 @@
/* 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/10/31 19:31:45 by jhalford ### ########.fr */ /* Updated: 2017/11/01 12:58:53 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
extern int g_rev;
void otool_mach_64(void *file, t_otooldata *data) void otool_mach_64(void *file, t_otooldata *data)
{ {
struct section_64 *sect; struct section_64 *sect;
DG("check");
if (data->flag & OTOOL_TEXT) if (data->flag & OTOOL_TEXT)
{ {
DG("check"); if ((sect = get_section_64(file, SECT_TEXT)))
if ((sect = get_section_64(file, SEG_TEXT, SECT_TEXT)))
{ {
DG("check");
ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT); ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT);
DG("check");
DG("hexdump: %i %i %i", sect->offset, sect->addr, sect->size);
hexdump_64(file + endian(sect->offset, 32), hexdump_64(file + endian(sect->offset, 32),
endian(sect->addr, 32), endian(sect->size, 32)); endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
DG("check");
} }
if (data->flag & OTOOL_DATA) if (data->flag & OTOOL_DATA)
{ {
if ((sect = get_section_64(file, SEG_DATA, SECT_DATA))) if ((sect = get_section_64(file, 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(file + endian(sect->offset, 32),
endian(sect->addr, 32), endian(sect->size, 32)); endian(sect->addr, 32), endian(sect->size, 32), g_rev);
} }
} }
} }

View file

@ -1,3 +1,15 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* symbol_64_format.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 12:37:07 by jhalford #+# #+# */
/* Updated: 2017/11/01 12:37:07 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
t_symbolmap g_symbolmap_64[] = t_symbolmap g_symbolmap_64[] =

View file

@ -0,0 +1,39 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* otool_ppc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2017/11/01 11:32:36 by jhalford #+# #+# */
/* Updated: 2017/11/01 12:58:28 by jhalford ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_nm_otool.h"
extern int g_rev;
void otool_ppc(void *file, t_otooldata *data)
{
struct section *sect;
if (data->flag & OTOOL_TEXT)
{
if ((sect = get_section(file, SECT_TEXT)))
{
ft_printf("Contents of (%s,%s) section\n", SEG_TEXT, SECT_TEXT);
hexdump_int(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)))
{
ft_printf("Contents of (%s,%s) section\n", SEG_DATA, SECT_DATA);
hexdump_int(file + endian(sect->offset, 32),
endian(sect->addr, 32), endian(sect->size, 32), g_rev);
}
}
}