added tests, starting parsing dysymtab

This commit is contained in:
Jack Halford 2017-03-01 17:56:02 +01:00
parent 79234f9ed9
commit 2189c15ce9
10 changed files with 79 additions and 21 deletions

View file

@ -6,7 +6,7 @@
# By: jhalford <jack@crans.org> +#+ +:+ +#+ # # By: jhalford <jack@crans.org> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ # # +#+#+#+#+#+ +#+ #
# Created: 2017/02/19 03:29:38 by jhalford #+# #+# # # Created: 2017/02/19 03:29:38 by jhalford #+# #+# #
# Updated: 2017/02/20 15:38:45 by jhalford ### ########.fr # # Updated: 2017/03/01 16:00:29 by jhalford ### ########.fr #
# # # #
# **************************************************************************** # # **************************************************************************** #

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/02/20 16:10:27 by jhalford ### ########.fr */ /* Updated: 2017/03/01 17:52:35 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -27,7 +27,7 @@
# define IS_MAGIC_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64) # define IS_MAGIC_64(x) (x == MH_MAGIC_64 || x == MH_CIGAM_64)
# define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM) # define IS_FAT(x) (x == FAT_MAGIC || x == FAT_CIGAM)
void dump_symtab(struct symtab_command *sym, void *file); void dump_symtab(struct symtab_command *sym, struct dysymtab_command *dysym, void *file);
void *hexdump(void *addr, unsigned int offset, unsigned int size); void *hexdump(void *addr, unsigned int offset, unsigned int size);
#endif #endif

View file

@ -6,23 +6,56 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 13:20:22 by jhalford #+# #+# */ /* Created: 2017/02/20 13:20:22 by jhalford #+# #+# */
/* Updated: 2017/02/20 15:03:38 by jhalford ### ########.fr */ /* Updated: 2017/03/01 17:55:11 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#include "ft_nm_otool.h" #include "ft_nm_otool.h"
void dump_symtab(struct symtab_command *sym, void *file) void dump_symtab(struct symtab_command *sym, struct dysymtab_command *dysym, void *file)
{ {
int i; int i;
int nsyms; int nsyms;
char *stringtable; char *stringtable;
struct nlist_64 *array; char *string;
struct nlist_64 *array;
struct dylib_reference *ref;
nsyms = sym->nsyms; nsyms = sym->nsyms;
array = file + sym->symoff; array = file + sym->symoff;
stringtable = (void*)file + sym->stroff; stringtable = file + sym->stroff;
ft_printf("{und}LC_SYMTAB w/ [%d] symbols:{eoc}\n", sym->nsyms); ft_printf("LC_SYMTAB [%d] symbols:\n", sym->nsyms);
for (i = 0; i < nsyms; ++i) i = 0;
while (++i < nsyms)
{
ft_printf("%s\n", stringtable + array[i].n_un.n_strx); ft_printf("%s\n", stringtable + array[i].n_un.n_strx);
}
ft_putendl("");
ft_printf("ilocalsym %i\n", dysym->ilocalsym);
ft_printf("nlocalsym %i\n", dysym->nlocalsym);
ft_printf("iextdefsym %i\n", dysym->iextdefsym);
ft_printf("nextdefsym %i\n", dysym->nextdefsym);
ft_printf("iundefsym %i\n", dysym->iundefsym);
ft_printf("nundefsym %i\n", dysym->nundefsym);
i = -1;
ft_printf("LC_DYSYMTAB ntoc=[%d]\n", dysym->ntoc);
ft_printf("LC_DYSYMTAB nmodtab=[%d]\n", dysym->nmodtab);
ft_printf("LC_DYSYMTAB nextrefsyms=[%d]\n", dysym->nextrefsyms);
ft_printf("LC_DYSYMTAB nmodtab=[%d]\n", dysym->nmodtab);
ft_printf("LC_DYSYMTAB nindirectsyms=[%d]\n", dysym->nindirectsyms);
ft_printf("LC_DYSYMTAB nextrel=[%d]\n", dysym->nextrel);
ft_printf("LC_DYSYMTAB nlocrel=[%d]\n", dysym->nlocrel);
ft_putendl("");
ref = file + dysym->indirectsymoff;
while (++i < (int)dysym->nindirectsyms)
{
string = stringtable + array[ref->isym].n_un.n_strx;
ft_printf("%i: %s,%#x\n", ref->isym, string, ref->flags);
ref += 1;
}
(void)file;
} }

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/02/20 16:45:26 by jhalford ### ########.fr */ /* Updated: 2017/03/01 17:53:31 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
@ -34,25 +34,34 @@ void dump_segment_64(struct segment_command_64 *seg, void *file)
} }
} }
void print_output(int nsyms, int symoff, int stroff, void *ptr);
void dump_mach_header_64(void *file) void dump_mach_header_64(void *file)
{ {
uint32_t ncmds; uint32_t ncmds;
uint32_t i; uint32_t i;
struct load_command *lc; struct load_command *lc;
struct symtab_command *symtab;
struct dysymtab_command *dysymtab;
struct mach_header_64 *header = file; struct mach_header_64 *header = file;
ncmds = header->ncmds; ncmds = header->ncmds;
lc = (void*)(header + 1); lc = (void*)(header + 1);
ft_printf("{blu}{inv}mach_header_64 w/ [%d] load_commands{eoc}\n", ncmds); /* ft_printf("{blu}{inv}mach_header_64 w/ [%d] load_commands{eoc}\n", ncmds); */
for (i = 0; i < ncmds; i++) for (i = 0; i < ncmds; i++)
{ {
ft_printf("{yel}{inv}load_command #%d: %#x{eoc}\n", i, lc->cmd); /* ft_printf("{yel}{inv}load_command #%d: %#x{eoc}\n", i, lc->cmd); */
if (lc->cmd & LC_SYMTAB) if (lc->cmd == LC_SYMTAB)
dump_symtab((struct symtab_command*)lc, file); symtab = (struct symtab_command*)lc;
else if (lc->cmd & LC_SEGMENT_64) /* dump_symtab((struct symtab_command*)lc, file); */
dump_segment_64((struct segment_command_64*)lc, file); else if (lc->cmd == LC_DYSYMTAB)
dysymtab = (struct dysymtab_command*)lc;
/* dump_dysymtab((struct dysymtab_command*)lc, file); */
/* else if (lc->cmd == LC_SEGMENT_64) */
/* dump_segment_64((struct segment_command_64*)lc, file); */
lc = (void*)lc + lc->cmdsize; lc = (void*)lc + lc->cmdsize;
} }
dump_symtab(symtab, dysymtab, file);
} }
void dump_fat_header(void *file) void dump_fat_header(void *file)

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/02/20 16:27:56 by jhalford ### ########.fr */ /* Updated: 2017/03/01 17:51:49 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

View file

@ -6,7 +6,7 @@
/* By: jhalford <jack@crans.org> +#+ +:+ +#+ */ /* By: jhalford <jack@crans.org> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2017/02/20 15:14:33 by jhalford #+# #+# */ /* Created: 2017/02/20 15:14:33 by jhalford #+# #+# */
/* Updated: 2017/02/20 16:52:26 by jhalford ### ########.fr */ /* Updated: 2017/03/01 15:37:46 by jhalford ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */

BIN
nm-otool/tests/test_facile Executable file

Binary file not shown.

View file

@ -0,0 +1,7 @@
#include <stdio.h>
int main(void)
{
puts("Test facile");
return (0);
}

BIN
nm-otool/tests/test_moins_facile Executable file

Binary file not shown.

View file

@ -0,0 +1,9 @@
#include <stdio.h>
int une_globale = 40;
int main(void)
{
printf("La globale vaut: %d\n", une_globale);
return (0);
}