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
SRC_BASE = \
bswap.c\
dump_symtab.c\
endianness.c\
fat.c\
ft_nm.c\
@ -57,7 +55,8 @@ 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
mach_64/symbol_64_sort.c\
ppc/otool_ppc.c
SRCS = $(addprefix $(SRC_DIR), $(SRC_BASE))
OBJS = $(addprefix $(OBJ_DIR), $($(notdir SRC_BASE):.c=.o))

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
};
void nm_single_file(void *file, t_nmdata *data);
#endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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.h"
# include "ppc.h"
# define IS_MACH_64(x) (*(uint32_t*)x == MH_MAGIC_64 || *(uint32_t*)x == MH_CIGAM_64)
# 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)
typedef unsigned int t_u;
uint64_t endian(uint64_t n, uint8_t size);
uint8_t bswap_8(uint8_t x);
uint16_t bswap_16(uint16_t x);
uint32_t bswap_32(uint32_t x);
uint64_t bswap_64(uint64_t x);
# 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;})
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_subtype_t cpusubtype);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
};
void otool_single_file(void *file, t_otooldata *data);
#endif

View file

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

View file

@ -6,39 +6,39 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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
typedef struct s_symbol_64 t_symbol_64;
struct s_symbol_64
struct s_symbol_64
{
int pos;
t_symtype type;
char *string;
struct nlist_64 nlist;
struct section_64 *section;
int pos;
t_symtype type;
char *string;
struct nlist_64 nlist;
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);
int symbol_64_set(t_symbol_64 *symbol, t_machodata *data);
void symbol_64_free(void *data, size_t size);
int symbol_64_sort(t_list **syms, t_flag flag);
int symbol_64_filter(t_list **syms, t_flag flag);
int is_external_64(t_symbol_64 *s);
int is_not_external_64(t_symbol_64 *s);
int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data);
void *get_section_64(void *file, char *segname,
char *sectname);
void mach_64_parse(t_machodata *data);
int symbol_64_set(t_symbol_64 *symbol, t_machodata *data);
void symbol_64_free(void *data, size_t size);
int symbol_64_sort(t_list **syms, t_flag flag);
int symbol_64_filter(t_list **syms, t_flag flag);
int is_external_64(t_symbol_64 *s);
int is_not_external_64(t_symbol_64 *s);
int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data);
void *get_section_64(struct mach_header_64 *file,
char *lookup);
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);
void otool_mach_64(void *file, t_otooldata *data);
void otool_mach_64(void *file, t_otooldata *data);
#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/push.c\
lst/top.c\
math/bswap.c\
math/ft_addrcmp.c\
math/ft_ilen.c\
math/ft_ilen_base.c\
@ -144,6 +145,7 @@ printing/ft_putendl.c\
printing/ft_putnbr.c\
printing/ft_putstr.c\
printing/hexdump.c\
printing/hexdump_int.c\
rs/rs.c\
sstr/ft_sstradd.c\
sstr/ft_sstrcat.c\

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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_hex_fd(long n, int fd);
void *hexdump(void *addr, uint32_t offset, uint32_t size);
void *hexdump_64(void *addr, uint64_t offset, uint64_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, 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);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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_DIST(a, b) FT_ABS((a) - (b))
char *ft_itoa(int n);
char *ft_itoa_base(int 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_uitoa_base(unsigned int nbr, char *base);
size_t ft_ilen(int n);
size_t ft_ilen_base(int n, int base);
size_t ft_uilen(unsigned int n);
size_t ft_lllen(long long n);
size_t ft_lllen_base(long long n, int base);
char *ft_itoa(int n);
char *ft_itoa_base(int 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_uitoa_base(unsigned int nbr, char *base);
size_t ft_ilen(int n);
size_t ft_ilen_base(int n, int base);
size_t ft_uilen(unsigned int n);
size_t ft_lllen(long long n);
size_t ft_lllen_base(long long n, int base);
int ft_addrcmp(void *a, void *b);
void *id(void *data);
int ft_addrcmp(void *a, void *b);
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

View file

@ -6,7 +6,7 @@
/* By: jhalford <marvin@42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
if (!*begin_list)
return ;
int i = 0;
while (*indirect && (*indirect)->next)
{
if ((*cmp)((*indirect)->content, (*indirect)->next->content) > 0)
@ -32,7 +31,6 @@ void ft_lstsort(t_list **begin_list, int (*cmp)())
*indirect = tmp2;
(*indirect)->next = tmp;
indirect = begin_list;
i = 0;
}
else
indirect = &(*indirect)->next;

View file

@ -6,11 +6,11 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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)
{
@ -19,8 +19,7 @@ inline uint8_t bswap_8(uint8_t x)
inline uint16_t bswap_16(uint16_t x)
{
return (( (x >> 8) & 0xffu) | (
(x & 0xffu) << 8));
return (((x >> 8) & 0xffu) | ((x & 0xffu) << 8));
}
inline uint32_t bswap_32(uint32_t x)

View file

@ -6,14 +6,14 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 "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;
@ -22,43 +22,43 @@ static void print_hex_contents(void *addr, uint64_t size)
{
if ((uint64_t)(a - addr) >= size)
break ;
else if (swap)
ft_printf("%02x", bswap_8(*(uint8_t*)a));
else
ft_printf("%02x", *(unsigned char*)a);
ft_printf("%02x", *(uint8_t*)a);
ft_putchar(' ');
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;
/* addr += offset; */
a = addr;
if (addr == NULL)
return (addr);
while ((uint32_t)(a - addr) < size)
{
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');
a += 16;
}
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;
/* 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)), swap);
ft_putchar('\n');
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
for file in $@; do
sed s/_64//g $@ > $(sed s/_64//g <<< "$@")
sed s/_64//g $file > $(sed s/_64//g <<< "$file")
done

View file

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

View file

@ -1,7 +1,7 @@
#!/usr/bin/env zsh
diff <(./ft_otool $@) <(otool $@)
if [ $? -eq 0 ]; then
echo "otool $@\t OK ✅ "
echo "OK ✅ otool $@"
else
echo "otool $@\t ERROR ✅ "
echo "ERROR ❎ otool $@"
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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,12 +16,12 @@ void *fat_extract(struct fat_header *fat,
cpu_type_t cputype,
cpu_subtype_t cpusubtype)
{
uint32_t narch;
uint32_t narch;
struct fat_arch *arch;
narch = endian(fat->nfat_arch, 32);
arch = (struct fat_arch*)(fat + 1);
while (narch--)
while (narch--)
{
if ((int32_t)endian(arch->cputype, 32) == cputype
&& (int32_t)endian(arch->cpusubtype, 32) & cpusubtype)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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,27 +33,26 @@ t_cliopts g_nm_opts[] =
{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)
{
uint32_t narch;
struct fat_arch *obj;
uint32_t narch;
struct fat_arch *obj;
const NXArchInfo *arch;
g_rev = IS_REV(fat);
obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL);
if (obj)
if ((obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL)))
nm_single_file(obj, data);
else
{
narch = endian(fat->nfat_arch, 32);
obj = (struct fat_arch*)(fat + 1);
while (narch--)
while (narch--)
{
const NXArchInfo *arch = NXGetArchInfoFromCpuType(
arch = NXGetArchInfoFromCpuType(
endian(obj->cputype, 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);
g_rev = IS_REV(fat);
++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)
{
const NXArchInfo *arch;
g_rev = IS_REV(file);
if (IS_MACH_32(file))
nm_mach(file, data);
@ -70,13 +71,13 @@ void nm_single_file(void *file, t_nmdata *data)
nm_mach_64(file, data);
else
{
const NXArchInfo *arch = NXGetArchInfoFromCpuType(
arch = NXGetArchInfoFromCpuType(
endian(*((int32_t*)file + 1), 32),
endian(*((int32_t*)file + 2), 32));
if (arch)
ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name);
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;
i = data.av_data - av;
while (i < ac && av[i])
while (i < ac && (data.filename = av[i]))
{
g_rev = 0;
data.filename = av[i];
if ((fd = open((data.filename), O_RDONLY)) < 0)
return (1);
if ((fstat(fd, &buf)) < 0)
if ((fd = open(data.filename, O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
return (1);
if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
== MAP_FAILED)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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;
void otool_single_file(void *file, t_otooldata *data);
void otool_fat_file(struct fat_header *fat, t_otooldata *data)
{
uint32_t narch;
struct fat_arch *obj;
uint32_t narch;
struct fat_arch *obj;
const NXArchInfo *arch;
g_rev = IS_REV(fat);
obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL);
if (obj)
if ((obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL)))
{
ft_printf("%s:\n", data->filename);
otool_single_file(obj, data);
}
else
{
narch = endian(fat->nfat_arch, 32);
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->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);
g_rev = IS_REV(fat);
++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)
{
const NXArchInfo *arch;
g_rev = IS_REV(file);
if (IS_MACH_32(file))
otool_mach(file, data);
else if (IS_MACH_64(file))
otool_mach_64(file, data);
else
arch = NXGetArchInfoFromCpuType(
endian(*((int32_t*)file + 1), 32),
endian(*((int32_t*)file + 2), 32));
if (arch)
{
const NXArchInfo *arch = NXGetArchInfoFromCpuType(
endian(*((int32_t*)file + 1), 32),
endian(*((int32_t*)file + 2), 32));
if (arch)
ft_printf("{red}%s unsupported architecture{eoc}\n", arch->name);
if (ft_strcmp(arch->name, "ppc") == 0)
otool_ppc(file, data);
else if (IS_MACH_32(file))
otool_mach(file, data);
else if (IS_MACH_64(file))
otool_mach_64(file, data);
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)
@ -75,12 +80,9 @@ int otool(int ac, char **av, t_otooldata data)
void *file;
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)
return (1);
if ((fstat(fd, &buf)) < 0)
if ((fd = open((av[i]), O_RDONLY)) < 0 || fstat(fd, &buf) < 0)
return (1);
if ((file = mmap(NULL, buf.st_size, PROT_READ, MAP_PRIVATE, fd, 0))
== MAP_FAILED)
@ -88,7 +90,10 @@ int otool(int ac, char **av, t_otooldata data)
if (IS_FAT(file))
otool_fat_file(file, &data);
else
{
ft_printf("%s:\n", data.filename);
otool_single_file(file, &data);
}
if (munmap(file, buf.st_size))
return (1);
i++;

View file

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

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* mach.c :+: :+: :+: */
/* mach.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 i;
struct load_command *lc;
struct mach_header *header;
struct mach_header *header;
header = data->file;
ncmds = endian(header->ncmds, 32);

View file

@ -1,12 +1,12 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* nm_mach.c :+: :+: :+: */
/* 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 */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
extern int g_rev;
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)))
if ((sect = get_section(file, 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 ((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);
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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
t_symbolmap g_symbolmap[] =

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
}
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;
struct load_command *lc;
struct segment_command_64 *seg;
struct section_64 *sect;
if (!(seg = get_segment_64(file, segname)))
return (NULL);
sect = (void*)(seg + 1);
j = -1;
while (++j < endian(seg->nsects, 32))
lc = (void*)(file + 1);
i = -1;
while (++i < endian(file->ncmds, 32))
{
if (ft_strcmp(sect->sectname, sectname) == 0)
return (sect);
sect = sect + 1;
if (endian(lc->cmd, 32) & LC_SEGMENT_64)
{
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);
}

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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);
array = (struct nlist_64*)(data->file + endian(symtab->symoff, 32));
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_set(&symbol, data);

View file

@ -6,38 +6,34 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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"
extern int g_rev;
void otool_mach_64(void *file, t_otooldata *data)
{
struct section_64 *sect;
DG("check");
if (data->flag & OTOOL_TEXT)
{
DG("check");
if ((sect = get_section_64(file, SEG_TEXT, SECT_TEXT)))
if ((sect = get_section_64(file, SECT_TEXT)))
{
DG("check");
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),
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 ((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);
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"
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);
}
}
}