fat binaries done, otool doesnt work on object files

This commit is contained in:
Jack Halford 2017-10-31 19:59:45 +01:00
parent 1103db79be
commit aecf9be263
26 changed files with 280 additions and 182 deletions

View file

@ -1,31 +0,0 @@
#!/bin/sh
make
##########
# NM
##########
#test_facile
./nmdiff.sh tests/test_facile
#test_moins_facile en 64 et 32
./nmdiff.sh tests/test_moins_facile
./nmdiff.sh tests/test_moins_facile_32-bit
#arguments mutilples
./nmdiff.sh tests/test_facile tests/test_facile
#fichiers objets
./nmdiff.sh objs/ft_nm.o
#bibliotheques dynamiques
./nmdiff.sh
#biblioteque universelle
./nmdiff.sh
##########
# OTOOL
##########

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/30 16:37:24 by jhalford ### ########.fr */ /* Updated: 2017/10/31 16:03:56 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -24,6 +24,7 @@
# include <mach-o/loader.h> # include <mach-o/loader.h>
# include <mach-o/nlist.h> # include <mach-o/nlist.h>
# include <mach-o/fat.h> # include <mach-o/fat.h>
# include <mach-o/arch.h>
# include "ft_nm.h" # include "ft_nm.h"
# include "ft_otool.h" # include "ft_otool.h"
@ -31,10 +32,10 @@
# include "mach_64.h" # include "mach_64.h"
# include "mach.h" # include "mach.h"
# define IS_MACH_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64) # define IS_MACH_64(x) (*(uint32_t*)x == MH_MAGIC_64 || *(uint32_t*)x == MH_CIGAM_64)
# define IS_MACH_32(x) (x == MH_MAGIC || x == MH_CIGAM) # define IS_MACH_32(x) (*(uint32_t*)x == MH_MAGIC || *(uint32_t*)x == MH_CIGAM)
# define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM) # define IS_FAT(x) (*(uint32_t*)x == FAT_MAGIC || *(uint32_t*)x == FAT_CIGAM)
# define IS_REV(x) (x == MH_CIGAM || x == MH_CIGAM_64 || 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); uint64_t endian(uint64_t n, uint8_t size);
uint8_t bswap_8(uint8_t x); uint8_t bswap_8(uint8_t x);

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/30 12:00:39 by jhalford ### ########.fr */ /* Updated: 2017/10/31 19:15:26 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,13 @@
# define OTOOL_TEXT (1 << 0) # define OTOOL_TEXT (1 << 0)
# define OTOOL_DATA (1 << 1) # define OTOOL_DATA (1 << 1)
typedef t_data_template t_otooldata; typedef struct s_otooldata t_otooldata;
struct s_otooldata
{
t_flag flag;
char **av_data;
char *filename;
};
#endif #endif

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */ /* Created: 2017/10/26 18:39:31 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:17:17 by jhalford ### ########.fr */ /* Updated: 2017/10/31 16:30:06 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@ 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;
}; };

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/30 12:22:12 by jhalford ### ########.fr */ /* Updated: 2017/10/31 16:30:06 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,7 +20,7 @@ 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;
}; };

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/01/09 12:29:58 by jhalford ### ########.fr */ /* Updated: 2017/10/31 19:05:46 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -21,6 +21,7 @@ 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)
@ -31,6 +32,7 @@ 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;

53
nm-otool/scripts/42corr.sh Executable file
View file

@ -0,0 +1,53 @@
#!/bin/sh
make
##########
# NM
##########
#test_facile
./scripts/nmdiff.sh tests/test_facile
#test_moins_facile en 64 et 32
./scripts/nmdiff.sh tests/test_moins_facile
./scripts/nmdiff.sh tests/test_moins_facile_32-bit
#arguments mutilples
./scripts/nmdiff.sh tests/test_facile tests/test_facile
#fichiers objets
./scripts/nmdiff.sh objs/ft_nm.o
#bibliotheques dynamiques
./scripts/nmdiff.sh /usr/lib/libcompression.dylib
#biblioteque universelle
./scripts/nmdiff.sh /usr/bin/audiodevice
./scripts/nmdiff.sh /usr/bin/python
##########
# OTOOL
##########
#test_facile
./scripts/otooldiff.sh -t tests/test_facile
#test_moins_facile en 64 et 32
./scripts/otooldiff.sh -t tests/test_moins_facile
./scripts/otooldiff.sh -t tests/test_moins_facile_32-bit
#arguments mutilples
./scripts/otooldiff.sh -t tests/test_facile tests/test_facile
#fichiers objets
./scripts/otooldiff.sh -t objs/ft_nm.o
#bibliotheques dynamiques
./scripts/otooldiff.sh -t /usr/lib/libcompression.dylib
#biblioteque universelle
./scripts/otooldiff.sh -t /usr/bin/audiodevice
./scripts/otooldiff.sh -t /usr/bin/python

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 "OK ✅" echo "nm $@\t OK ✅ "
else else
echo "ERROR ❎" echo "nm $@\t ERROR ✅ "
fi fi

6
nm-otool/scripts/nmdiff_dir.sh Executable file
View file

@ -0,0 +1,6 @@
#!/bin/sh
for file in $@/*; do
printf "%-40s" $file
./nmdiff.sh $file 2>&-
done

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 "OK ✅" echo "otool $@\t OK ✅ "
else else
echo "ERROR ❎" echo "otool $@\t ERROR ✅ "
fi fi

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/30 17:49:28 by jhalford ### ########.fr */ /* Updated: 2017/10/31 17:45:48 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -23,11 +23,9 @@ void *fat_extract(struct fat_header *fat,
arch = (struct fat_arch*)(fat + 1); arch = (struct fat_arch*)(fat + 1);
while (narch--) while (narch--)
{ {
if (endian(arch->cputype, 32) & cputype if ((int32_t)endian(arch->cputype, 32) == cputype
&& endian(arch->cpusubtype, 32) & cpusubtype) && (int32_t)endian(arch->cpusubtype, 32) & cpusubtype)
{
return ((void *)fat + endian(arch->offset, 32)); return ((void *)fat + endian(arch->offset, 32));
}
++arch; ++arch;
} }
return (NULL); return (NULL);

View file

@ -6,12 +6,11 @@
/* 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/30 18:36:32 by jhalford ### ########.fr */ /* Updated: 2017/10/31 19:45:02 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
#include <sys/utsname.h>
#define NM_USAGE "usage: nm [-agmnpruU] filename ..." #define NM_USAGE "usage: nm [-agmnpruU] filename ..."
t_machodata *g_data = NULL; t_machodata *g_data = NULL;
@ -31,45 +30,54 @@ t_cliopts g_nm_opts[] =
{0xff, "full", NM_FULL, 0, NULL, 0}, {0xff, "full", NM_FULL, 0, NULL, 0},
{'o', NULL, NM_OFORMAT, 0, NULL, 0}, {'o', NULL, NM_OFORMAT, 0, NULL, 0},
{'m', NULL, NM_MFORMAT, 0, NULL, 0}, {'m', NULL, NM_MFORMAT, 0, NULL, 0},
{0, 0, 0, 0, 0, 0},
}; };
int nm_file(void *file, t_nmdata *data); void nm_single_file(void *file, t_nmdata *data);
int nm_fat(struct fat_header *fat, t_nmdata *data) void nm_fat_file(struct fat_header *fat, t_nmdata *data)
{ {
void *arch; uint32_t narch;
struct utsname host; struct fat_arch *obj;
uname(&host); g_rev = IS_REV(fat);
NXGetLocalArchInfo(); obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL);
if (obj)
arch = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL); nm_single_file(obj, data);
if (!arch)
ft_printf("{red}fat doesn't have x86_64 !{eoc}");
else else
{ {
uint32_t magic; narch = endian(fat->nfat_arch, 32);
magic = *(int*)arch; obj = (struct fat_arch*)(fat + 1);
nm_file(arch, data); while (narch--)
{
const NXArchInfo *arch = NXGetArchInfoFromCpuType(
endian(obj->cputype, 32),
endian(obj->cpusubtype, 32));
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;
}
} }
return (0);
} }
int nm_file(void *file, t_nmdata *data) void nm_single_file(void *file, t_nmdata *data)
{ {
uint32_t magic; g_rev = IS_REV(file);
if (IS_MACH_32(file))
magic = *(int*)file;
g_rev = IS_REV(magic);
if (IS_MACH_32(magic))
nm_mach(file, data); nm_mach(file, data);
else if (IS_MACH_64(magic)) else if (IS_MACH_64(file))
nm_mach_64(file, data); nm_mach_64(file, data);
else if (IS_FAT(magic))
nm_fat(file, data);
else else
ft_printf("{red}unsupported arch:{eoc} magic=%#x\n", magic); {
return (0); 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);
else
ft_dprintf(2, "unknown architecture, magic=%#x\n", *((int32_t*)file));
}
} }
int nm(int ac, char **av, t_nmdata data) int nm(int ac, char **av, t_nmdata data)
@ -77,14 +85,13 @@ int nm(int ac, char **av, t_nmdata data)
int i; int i;
struct stat buf; struct stat buf;
int fd; int fd;
char *file; void *file;
i = data.av_data - av; i = data.av_data - av;
while (i < ac && av[i]) while (i < ac && av[i])
{ {
g_rev = 0;
data.filename = av[i]; data.filename = av[i];
if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1)
ft_printf("%c%s:\n", i - (data.av_data - av) ? '\n' : 0, av[i]);
if ((fd = open((data.filename), O_RDONLY)) < 0) if ((fd = open((data.filename), O_RDONLY)) < 0)
return (1); return (1);
if ((fstat(fd, &buf)) < 0) if ((fstat(fd, &buf)) < 0)
@ -92,7 +99,12 @@ int nm(int ac, char **av, t_nmdata data)
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)
return (1); return (1);
nm_file(file, &data); if (!(data.flag & NM_OFORMAT) && ac - (data.av_data - av) > 1)
ft_printf("\n%s:\n", data.filename);
if (IS_FAT(file))
nm_fat_file(file, &data);
else
nm_single_file(file, &data);
if (munmap(file, buf.st_size)) if (munmap(file, buf.st_size))
return (1); return (1);
i++; i++;

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/30 17:31:44 by jhalford ### ########.fr */ /* Updated: 2017/10/31 19:45:03 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,20 +20,51 @@ t_cliopts g_otool_opts[] =
}; };
int g_rev = 0; int g_rev = 0;
void otool_file(void *file, t_otooldata *data) void otool_single_file(void *file, t_otooldata *data);
{
uint32_t magic;
magic = *(int *)file; void otool_fat_file(struct fat_header *fat, t_otooldata *data)
g_rev = IS_REV(magic); {
if (IS_MACH_32(magic)) uint32_t narch;
otool_mach(file, data); struct fat_arch *obj;
else if (IS_MACH_64(magic))
otool_mach_64(file, data); g_rev = IS_REV(fat);
else if (IS_FAT(magic)) obj = fat_extract(fat, CPU_TYPE_X86_64, CPU_SUBTYPE_X86_64_ALL);
ft_printf("fat binary not supported yet\n"); if (obj)
otool_single_file(obj, data);
else else
ft_printf("{red}unsupported architecture:{eoc} magic = %#x\n", magic); {
narch = endian(fat->nfat_arch, 32);
obj = (struct fat_arch*)(fat + 1);
while (narch--)
{
const NXArchInfo *arch = NXGetArchInfoFromCpuType(
endian(obj->cputype, 32),
endian(obj->cpusubtype, 32));
ft_printf("\n%s (for architecture %s):\n", data->filename, arch->name);
otool_single_file((void*)fat + endian(obj->offset, 32), data);
g_rev = IS_REV(fat);
++obj;
}
}
}
void otool_single_file(void *file, t_otooldata *data)
{
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
{
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);
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)
@ -41,7 +72,7 @@ int otool(int ac, char **av, t_otooldata data)
int i; int i;
struct stat buf; struct stat buf;
int fd; int fd;
char *file; void *file;
i = data.av_data - av; i = data.av_data - av;
while (i < ac && av[i]) while (i < ac && av[i])
@ -54,7 +85,10 @@ int otool(int ac, char **av, t_otooldata data)
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)
return (1); return (1);
otool_file(file, &data); if (IS_FAT(file))
otool_fat_file(file, &data);
else
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

@ -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/30 11:32:11 by jhalford ### ########.fr */ /* Updated: 2017/10/31 17:46:14 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -20,10 +20,10 @@ static void symtab_parse(t_machodata *data, struct symtab_command *symtab)
struct nlist *array; struct nlist *array;
data->symtab = symtab; data->symtab = symtab;
stringtable = data->file + symtab->stroff; stringtable = data->file + endian(symtab->stroff, 32);
array = (struct nlist*)(data->file + symtab->symoff); array = (struct nlist*)(data->file + endian(symtab->symoff, 32));
i = -1; i = -1;
while (++i < (int)symtab->nsyms) while (++i < (int)endian(symtab->nsyms, 32))
{ {
symbol_init(&symbol, stringtable, array, i); symbol_init(&symbol, stringtable, array, i);
symbol_set(&symbol, data); symbol_set(&symbol, data);
@ -35,9 +35,9 @@ static void seg_parse(t_machodata *data, struct segment_command *seg)
{ {
uint32_t nsects; uint32_t nsects;
uint32_t i; uint32_t i;
struct section *sect; struct section *sect;
nsects = seg->nsects; nsects = endian(seg->nsects, 32);
sect = (void*)(seg + 1); sect = (void*)(seg + 1);
i = -1; i = -1;
while (++i < nsects) while (++i < nsects)
@ -55,17 +55,15 @@ void mach_parse(t_machodata *data)
struct mach_header *header; struct mach_header *header;
header = data->file; header = data->file;
ncmds = header->ncmds; ncmds = endian(header->ncmds, 32);
lc = (void*)(header + 1); lc = (void*)(header + 1);
i = -1; i = -1;
while (++i < ncmds) while (++i < ncmds)
{ {
if (lc->cmd == LC_SYMTAB) if (endian(lc->cmd, 32) == LC_SYMTAB)
symtab_parse(data, (struct symtab_command*)lc); symtab_parse(data, (struct symtab_command*)lc);
else if (lc->cmd == LC_DYSYMTAB) else if (endian(lc->cmd, 32) == LC_SEGMENT)
data->dysymtab = (struct dysymtab_command*)lc;
else if (lc->cmd == LC_SEGMENT)
seg_parse(data, (struct segment_command*)lc); seg_parse(data, (struct segment_command*)lc);
lc = (void*)lc + lc->cmdsize; lc = (void*)lc + endian(lc->cmdsize, 32);
} }
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */ /* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:44:51 by jhalford ### ########.fr */ /* Updated: 2017/10/31 16:36:23 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,12 +19,12 @@ int cmp_symtype(t_symbol *sym, void *type)
inline int is_not_external(t_symbol *s) inline int is_not_external(t_symbol *s)
{ {
return (!(s->nlist->n_type & N_EXT)); return (!(s->nlist.n_type & N_EXT));
} }
inline int is_external(t_symbol *s) inline int is_external(t_symbol *s)
{ {
return (s->nlist->n_type & N_EXT); return (s->nlist.n_type & N_EXT);
} }
int symbol_filter(t_list **symbols, t_flag flag) int symbol_filter(t_list **symbols, t_flag flag)

View file

@ -15,14 +15,14 @@ t_symbolmap g_symbolmap[] =
void symbol_format_desc(t_symbol *symbol) void symbol_format_desc(t_symbol *symbol)
{ {
if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY) if (symbol->nlist.n_desc & REFERENCED_DYNAMICALLY)
ft_printf(" [referenced dynamically]"); ft_printf(" [referenced dynamically]");
if (is_external(symbol)) if (is_external(symbol))
ft_printf(" external"); ft_printf(" external");
else else
{ {
ft_printf(" non-external"); ft_printf(" non-external");
if (symbol->nlist->n_type & N_PEXT) if (symbol->nlist.n_type & N_PEXT)
ft_printf(" (was a private external)"); ft_printf(" (was a private external)");
} }
} }
@ -43,12 +43,12 @@ void symbol_format_full(t_symbol *symbol)
{ {
ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x", ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x",
symbol->pos, symbol->pos,
(symbol->nlist->n_type & N_STAB) >> 5, (symbol->nlist.n_type & N_STAB) >> 5,
(symbol->nlist->n_type & N_PEXT) >> 4, (symbol->nlist.n_type & N_PEXT) >> 4,
symbol->nlist->n_type & N_TYPE, symbol->nlist.n_type & N_TYPE,
symbol->nlist->n_type & N_EXT, symbol->nlist.n_type & N_EXT,
symbol->nlist->n_sect, symbol->section->sectname, symbol->nlist.n_sect, symbol->section->sectname,
symbol->nlist->n_desc); symbol->nlist.n_desc);
} }
void symbol_format_dfl(t_symbol *symbol) void symbol_format_dfl(t_symbol *symbol)
@ -62,8 +62,8 @@ void symbol_format_dfl(t_symbol *symbol)
ft_printf(" %c", map.c + 'a' - 'A'); ft_printf(" %c", map.c + 'a' - 'A');
if (symbol->type == SYM_STAB) if (symbol->type == SYM_STAB)
ft_printf(" %02x %04b %#x", ft_printf(" %02x %04b %#x",
symbol->nlist->n_sect, symbol->nlist->n_desc, symbol->nlist.n_sect, symbol->nlist.n_desc,
symbol->nlist->n_type); symbol->nlist.n_type);
} }
int symbol_format(t_symbol *symbol, t_nmdata *data) int symbol_format(t_symbol *symbol, t_nmdata *data)
@ -75,7 +75,7 @@ int symbol_format(t_symbol *symbol, t_nmdata *data)
if (symbol->type == SYM_UNDF) if (symbol->type == SYM_UNDF)
ft_printf("%8s", " "); ft_printf("%8s", " ");
else else
ft_printf("%08llx", symbol->nlist->n_value); ft_printf("%08llx", symbol->nlist.n_value);
if (data->flag & NM_MFORMAT) if (data->flag & NM_MFORMAT)
symbol_format_m(symbol); symbol_format_m(symbol);
else if (data->flag & NM_FULL) else if (data->flag & NM_FULL)

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */ /* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:23:24 by jhalford ### ########.fr */ /* Updated: 2017/10/31 16:42:20 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,17 +19,20 @@ int symbol_init(t_symbol *symbol,
{ {
symbol->type = 0; symbol->type = 0;
symbol->pos = i; symbol->pos = i;
symbol->nlist = array + i; symbol->nlist.n_type = endian(array[i].n_type, 8);
symbol->string = stringtable + array[i].n_un.n_strx; symbol->nlist.n_sect = endian(array[i].n_sect, 8);
symbol->nlist.n_desc = endian(array[i].n_desc, 16);
symbol->nlist.n_value = endian(array[i].n_value, 32);
symbol->string = stringtable + endian(array[i].n_un.n_strx, 32);
return (0); return (0);
} }
static int symbol_gettype(int type_mask, static int symbol_gettype(int type_mask,
struct nlist *nlist, struct section *section) struct nlist nlist, struct section *section)
{ {
if (nlist->n_type & N_STAB) if (nlist.n_type & N_STAB)
return (SYM_STAB); return (SYM_STAB);
else if (type_mask == N_UNDF && nlist->n_type & N_EXT && nlist->n_value) else if (type_mask == N_UNDF && nlist.n_type & N_EXT && nlist.n_value)
return (SYM_COMMON); return (SYM_COMMON);
else if (type_mask == N_UNDF) else if (type_mask == N_UNDF)
return (SYM_UNDF); return (SYM_UNDF);
@ -51,15 +54,13 @@ int symbol_set(t_symbol *sym, t_machodata *data)
{ {
t_list *lst; t_list *lst;
if (sym->nlist->n_sect) if (sym->nlist.n_sect &&
{ (lst = ft_lst_at(data->sects, sym->nlist.n_sect - 1)))
lst = ft_lst_at(data->sects, sym->nlist->n_sect - 1);
sym->section = *(struct section**)(lst->content); sym->section = *(struct section**)(lst->content);
}
else else
sym->section = NULL; sym->section = NULL;
sym->type = symbol_gettype( sym->type = symbol_gettype(
sym->nlist->n_type & N_TYPE, sym->nlist.n_type & N_TYPE,
sym->nlist, sym->nlist,
sym->section); sym->section);
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */ /* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:46:04 by jhalford ### ########.fr */ /* Updated: 2017/10/31 16:32:46 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,9 +19,9 @@ static int sym_asort(t_symbol *sym1, t_symbol *sym2)
static int sym_nsort(t_symbol *sym1, t_symbol *sym2) static int sym_nsort(t_symbol *sym1, t_symbol *sym2)
{ {
if (sym1->nlist->n_value == sym2->nlist->n_value) if (sym1->nlist.n_value == sym2->nlist.n_value)
return (sym_asort(sym1, sym2)); return (sym_asort(sym1, sym2));
return (sym1->nlist->n_value > sym2->nlist->n_value ? 1 : -1); return (sym1->nlist.n_value > sym2->nlist.n_value ? 1 : -1);
} }
int symbol_sort(t_list **syms, t_flag flag) int symbol_sort(t_list **syms, t_flag flag)

View file

@ -6,29 +6,34 @@
/* 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/10/31 19:58:07 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
void *get_segment_64(void *file, char *lookup) void *get_segment_64(struct mach_header_64 *file, char *lookup)
{ {
uint32_t i; uint32_t i;
struct load_command *lc; struct load_command *lc;
struct segment_command_64 *seg; struct segment_command_64 *seg;
uint32_t ncmds;
lc = (void*)((struct mach_header_64*)file + 1); lc = (void*)(file + 1);
i = -1; i = -1;
while (++i < ((struct mach_header_64*)file)->ncmds) ncmds = endian(file->ncmds, 32);
DG("ncmds=%i", ncmds);
while (++i < ncmds)
{ {
if (lc->cmd & LC_SEGMENT_64) DG("i=%i", i);
if (endian(lc->cmd, 32) & LC_SEGMENT_64)
{ {
seg = (struct segment_command_64*)lc; seg = (struct segment_command_64*)lc;
DG("segment: %s", seg->segname);
if (ft_strcmp(seg->segname, lookup) == 0) if (ft_strcmp(seg->segname, lookup) == 0)
return (seg); return (seg);
} }
lc = (void*)lc + lc->cmdsize; lc = (void*)lc + endian(lc->cmdsize, 32);
} }
return (NULL); return (NULL);
} }
@ -39,10 +44,11 @@ void *get_section_64(void *file, char *segname, char *sectname)
struct segment_command_64 *seg; struct segment_command_64 *seg;
struct section_64 *sect; struct section_64 *sect;
seg = get_segment_64(file, segname); if (!(seg = get_segment_64(file, segname)))
return (NULL);
sect = (void*)(seg + 1); sect = (void*)(seg + 1);
j = -1; j = -1;
while (++j < seg->nsects) while (++j < endian(seg->nsects, 32))
{ {
if (ft_strcmp(sect->sectname, sectname) == 0) if (ft_strcmp(sect->sectname, sectname) == 0)
return (sect); return (sect);

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/30 16:58:03 by jhalford ### ########.fr */ /* Updated: 2017/10/31 18:06:12 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)symtab->nsyms) 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);
@ -62,7 +62,7 @@ void mach_64_parse(t_machodata *data)
{ {
if (endian(lc->cmd, 32) == LC_SYMTAB) if (endian(lc->cmd, 32) == LC_SYMTAB)
symtab_64_parse(data, (struct symtab_command*)lc); symtab_64_parse(data, (struct symtab_command*)lc);
else if (lc->cmd == LC_SEGMENT_64) else if (endian(lc->cmd, 32) == LC_SEGMENT_64)
seg_64_parse(data, (struct segment_command_64*)lc); seg_64_parse(data, (struct segment_command_64*)lc);
lc = (void*)lc + endian(lc->cmdsize, 32); lc = (void*)lc + endian(lc->cmdsize, 32);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/30 11:04:06 by jhalford #+# #+# */ /* Created: 2017/10/30 11:04:06 by jhalford #+# #+# */
/* Updated: 2017/10/30 12:16:36 by jhalford ### ########.fr */ /* Updated: 2017/10/31 19:31:45 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -16,20 +16,28 @@ 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, SEG_TEXT, 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);
hexdump_64(file + sect->offset, sect->addr, sect->size); 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));
} }
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, SEG_DATA, 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 + sect->offset, sect->addr, sect->size); hexdump_64(file + endian(sect->offset, 32),
endian(sect->addr, 32), endian(sect->size, 32));
} }
} }
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */ /* Created: 2017/10/26 18:31:27 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:44:51 by jhalford ### ########.fr */ /* Updated: 2017/10/31 16:36:55 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,12 +19,12 @@ int cmp_symtype_64(t_symbol_64 *sym, void *type)
inline int is_not_external_64(t_symbol_64 *s) inline int is_not_external_64(t_symbol_64 *s)
{ {
return (!(s->nlist->n_type & N_EXT)); return (!(s->nlist.n_type & N_EXT));
} }
inline int is_external_64(t_symbol_64 *s) inline int is_external_64(t_symbol_64 *s)
{ {
return (s->nlist->n_type & N_EXT); return (s->nlist.n_type & N_EXT);
} }
int symbol_64_filter(t_list **symbols, t_flag flag) int symbol_64_filter(t_list **symbols, t_flag flag)

View file

@ -15,14 +15,14 @@ t_symbolmap g_symbolmap_64[] =
void symbol_64_format_desc(t_symbol_64 *symbol) void symbol_64_format_desc(t_symbol_64 *symbol)
{ {
if (symbol->nlist->n_desc & REFERENCED_DYNAMICALLY) if (symbol->nlist.n_desc & REFERENCED_DYNAMICALLY)
ft_printf(" [referenced dynamically]"); ft_printf(" [referenced dynamically]");
if (is_external_64(symbol)) if (is_external_64(symbol))
ft_printf(" external"); ft_printf(" external");
else else
{ {
ft_printf(" non-external"); ft_printf(" non-external");
if (symbol->nlist->n_type & N_PEXT) if (symbol->nlist.n_type & N_PEXT)
ft_printf(" (was a private external)"); ft_printf(" (was a private external)");
} }
} }
@ -43,12 +43,12 @@ void symbol_64_format_full(t_symbol_64 *symbol)
{ {
ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x", ft_printf("\t%i %03b|%b|%x|%b \t%i(%s) \t%04x",
symbol->pos, symbol->pos,
(symbol->nlist->n_type & N_STAB) >> 5, (symbol->nlist.n_type & N_STAB) >> 5,
(symbol->nlist->n_type & N_PEXT) >> 4, (symbol->nlist.n_type & N_PEXT) >> 4,
symbol->nlist->n_type & N_TYPE, symbol->nlist.n_type & N_TYPE,
symbol->nlist->n_type & N_EXT, symbol->nlist.n_type & N_EXT,
symbol->nlist->n_sect, symbol->section->sectname, symbol->nlist.n_sect, symbol->section->sectname,
symbol->nlist->n_desc); symbol->nlist.n_desc);
} }
void symbol_64_format_dfl(t_symbol_64 *symbol) void symbol_64_format_dfl(t_symbol_64 *symbol)
@ -62,8 +62,8 @@ void symbol_64_format_dfl(t_symbol_64 *symbol)
ft_printf(" %c", map.c + 'a' - 'A'); ft_printf(" %c", map.c + 'a' - 'A');
if (symbol->type == SYM_STAB) if (symbol->type == SYM_STAB)
ft_printf(" %02x %04b %#x", ft_printf(" %02x %04b %#x",
symbol->nlist->n_sect, symbol->nlist->n_desc, symbol->nlist.n_sect, symbol->nlist.n_desc,
symbol->nlist->n_type); symbol->nlist.n_type);
} }
int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data) int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data)
@ -75,7 +75,7 @@ int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data)
if (symbol->type == SYM_UNDF) if (symbol->type == SYM_UNDF)
ft_printf("%16s", " "); ft_printf("%16s", " ");
else else
ft_printf("%016llx", symbol->nlist->n_value); ft_printf("%016llx", symbol->nlist.n_value);
if (data->flag & NM_MFORMAT) if (data->flag & NM_MFORMAT)
symbol_64_format_m(symbol); symbol_64_format_m(symbol);
else if (data->flag & NM_FULL) else if (data->flag & NM_FULL)
@ -83,6 +83,8 @@ int symbol_64_format(t_symbol_64 *symbol, t_nmdata *data)
else else
symbol_64_format_dfl(symbol); symbol_64_format_dfl(symbol);
} }
ft_printf(" %s\n", symbol->string); ft_putchar(' ');
ft_putstr(symbol->string);
ft_putchar('\n');
return (0); return (0);
} }

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */ /* Created: 2017/10/26 18:07:28 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:23:24 by jhalford ### ########.fr */ /* Updated: 2017/10/31 17:52:38 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,17 +19,20 @@ int symbol_64_init(t_symbol_64 *symbol,
{ {
symbol->type = 0; symbol->type = 0;
symbol->pos = i; symbol->pos = i;
symbol->nlist = array + i; symbol->nlist.n_type = endian(array[i].n_type, 8);
symbol->string = stringtable + array[i].n_un.n_strx; symbol->nlist.n_sect = endian(array[i].n_sect, 8);
symbol->nlist.n_desc = endian(array[i].n_desc, 16);
symbol->nlist.n_value = endian(array[i].n_value, 64);
symbol->string = stringtable + endian(array[i].n_un.n_strx, 32);
return (0); return (0);
} }
static int symbol_64_gettype(int type_mask, static int symbol_64_gettype(int type_mask,
struct nlist_64 *nlist, struct section_64 *section) struct nlist_64 nlist, struct section_64 *section)
{ {
if (nlist->n_type & N_STAB) if (nlist.n_type & N_STAB)
return (SYM_STAB); return (SYM_STAB);
else if (type_mask == N_UNDF && nlist->n_type & N_EXT && nlist->n_value) else if (type_mask == N_UNDF && nlist.n_type & N_EXT && nlist.n_value)
return (SYM_COMMON); return (SYM_COMMON);
else if (type_mask == N_UNDF) else if (type_mask == N_UNDF)
return (SYM_UNDF); return (SYM_UNDF);
@ -51,15 +54,13 @@ int symbol_64_set(t_symbol_64 *sym, t_machodata *data)
{ {
t_list *lst; t_list *lst;
if (sym->nlist->n_sect) if (sym->nlist.n_sect &&
{ (lst = ft_lst_at(data->sects, sym->nlist.n_sect - 1)))
lst = ft_lst_at(data->sects, sym->nlist->n_sect - 1);
sym->section = *(struct section_64**)(lst->content); sym->section = *(struct section_64**)(lst->content);
}
else else
sym->section = NULL; sym->section = NULL;
sym->type = symbol_64_gettype( sym->type = symbol_64_gettype(
sym->nlist->n_type & N_TYPE, sym->nlist.n_type & N_TYPE,
sym->nlist, sym->nlist,
sym->section); sym->section);
return (0); return (0);

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */ /* Created: 2017/03/27 20:33:02 by jhalford #+# #+# */
/* Updated: 2017/10/26 18:46:04 by jhalford ### ########.fr */ /* Updated: 2017/10/31 19:05:57 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -19,9 +19,9 @@ static int sym_asort(t_symbol_64 *sym1, t_symbol_64 *sym2)
static int sym_nsort(t_symbol_64 *sym1, t_symbol_64 *sym2) static int sym_nsort(t_symbol_64 *sym1, t_symbol_64 *sym2)
{ {
if (sym1->nlist->n_value == sym2->nlist->n_value) if (sym1->nlist.n_value == sym2->nlist.n_value)
return (sym_asort(sym1, sym2)); return (ft_strcmp(sym1->string, sym2->string));
return (sym1->nlist->n_value > sym2->nlist->n_value ? 1 : -1); return (sym1->nlist.n_value - sym2->nlist.n_value);
} }
int symbol_64_sort(t_list **syms, t_flag flag) int symbol_64_sort(t_list **syms, t_flag flag)