From 2189c15ce9811c71b9838f5e930f2d10b5601790 Mon Sep 17 00:00:00 2001 From: Jack Halford Date: Wed, 1 Mar 2017 17:56:02 +0100 Subject: [PATCH] added tests, starting parsing dysymtab --- nm-otool/Makefile | 2 +- nm-otool/includes/ft_nm_otool.h | 4 +-- nm-otool/src/dump_symtab.c | 51 ++++++++++++++++++++++++----- nm-otool/src/ft_nm.c | 23 +++++++++---- nm-otool/src/ft_otool.c | 2 +- nm-otool/src/hexdump.c | 2 +- nm-otool/tests/test_facile | Bin 0 -> 8432 bytes nm-otool/tests/test_facile.c | 7 ++++ nm-otool/tests/test_moins_facile | Bin 0 -> 8480 bytes nm-otool/tests/test_moins_facile.c | 9 +++++ 10 files changed, 79 insertions(+), 21 deletions(-) create mode 100755 nm-otool/tests/test_facile create mode 100644 nm-otool/tests/test_facile.c create mode 100755 nm-otool/tests/test_moins_facile create mode 100644 nm-otool/tests/test_moins_facile.c diff --git a/nm-otool/Makefile b/nm-otool/Makefile index 1c1dfcce..76ed4024 100644 --- a/nm-otool/Makefile +++ b/nm-otool/Makefile @@ -6,7 +6,7 @@ # 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 # # # # **************************************************************************** # diff --git a/nm-otool/includes/ft_nm_otool.h b/nm-otool/includes/ft_nm_otool.h index fabf0927..99a5ef20 100644 --- a/nm-otool/includes/ft_nm_otool.h +++ b/nm-otool/includes/ft_nm_otool.h @@ -6,7 +6,7 @@ /* 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_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); #endif diff --git a/nm-otool/src/dump_symtab.c b/nm-otool/src/dump_symtab.c index 9bc878d1..4e26ffa2 100644 --- a/nm-otool/src/dump_symtab.c +++ b/nm-otool/src/dump_symtab.c @@ -6,23 +6,56 @@ /* 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" -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 nsyms; - char *stringtable; - struct nlist_64 *array; + int i; + int nsyms; + char *stringtable; + char *string; + struct nlist_64 *array; + struct dylib_reference *ref; nsyms = sym->nsyms; array = file + sym->symoff; - stringtable = (void*)file + sym->stroff; - ft_printf("{und}LC_SYMTAB w/ [%d] symbols:{eoc}\n", sym->nsyms); - for (i = 0; i < nsyms; ++i) + stringtable = file + sym->stroff; + ft_printf("LC_SYMTAB [%d] symbols:\n", sym->nsyms); + i = 0; + while (++i < nsyms) + { 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; } diff --git a/nm-otool/src/ft_nm.c b/nm-otool/src/ft_nm.c index 7c65ba42..e68ba7a1 100644 --- a/nm-otool/src/ft_nm.c +++ b/nm-otool/src/ft_nm.c @@ -6,7 +6,7 @@ /* 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) { uint32_t ncmds; uint32_t i; struct load_command *lc; + struct symtab_command *symtab; + struct dysymtab_command *dysymtab; struct mach_header_64 *header = file; ncmds = header->ncmds; 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++) { - ft_printf("{yel}{inv}load_command #%d: %#x{eoc}\n", i, lc->cmd); - if (lc->cmd & LC_SYMTAB) - dump_symtab((struct symtab_command*)lc, file); - else if (lc->cmd & LC_SEGMENT_64) - dump_segment_64((struct segment_command_64*)lc, file); + /* ft_printf("{yel}{inv}load_command #%d: %#x{eoc}\n", i, lc->cmd); */ + if (lc->cmd == LC_SYMTAB) + symtab = (struct symtab_command*)lc; + /* dump_symtab((struct symtab_command*)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; } + dump_symtab(symtab, dysymtab, file); } void dump_fat_header(void *file) diff --git a/nm-otool/src/ft_otool.c b/nm-otool/src/ft_otool.c index e164d8b6..250a0b84 100644 --- a/nm-otool/src/ft_otool.c +++ b/nm-otool/src/ft_otool.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/src/hexdump.c b/nm-otool/src/hexdump.c index ca6c12c3..617e7844 100644 --- a/nm-otool/src/hexdump.c +++ b/nm-otool/src/hexdump.c @@ -6,7 +6,7 @@ /* 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 */ /* */ /* ************************************************************************** */ diff --git a/nm-otool/tests/test_facile b/nm-otool/tests/test_facile new file mode 100755 index 0000000000000000000000000000000000000000..58a820c341a8036ca8ac112d9e66477de2a9d170 GIT binary patch literal 8432 zcmeHM&ubGw6rQwIOIz*sBL1qZ#fo}pE2sz}G}2&-MO&MK6?B@UTN6mqHoMXGRxE-! z6#6H46i*(DCq=~{7q1?@C>0buiiq+1W@nS#RJ{r^4_@B9d2haX^UW-5cHVyf@^gm} zu~s2gT7(eY&_``T+!Z!jLhOYmp;C^=FC?xdu3TnsRAN#0w;plcAyLXy;%X|IqQ+a% zKB0ZoCbns1hLoPOV#&zhZ?U-$LWV9f{lQbmD25e zX;$V-bMwlN?#G74V%74TQV0b<_J`nP+=F=Tg7SeyivoN0d?K+=+7?0;J>^Gi+vC&Hx6CbGO6pp|8cZg*y5}u*@U(jOXC<9B;s$f$fBjL&L({K*Rmv z_5-`=dC9xUWDgR{$pS@@LwP}cF}>EghtTc_iXS8u$?PW4V7MxO;0 zogGS#2R{2Ccn()Mj?ZCOI1kss7;BDoyutEfjkz)ZevB9f3GHFkl!k3>XIfH3L)0>WAd&XDhjO?ks9~nfSqqHh1FZ#OrF} zTlH0P^|^KZS*`yL17d8gcM&E&S?f>3uIJeK>0_-{OF6D*&!w~Zf`hIY^Z;X!u;sfn z-?dM|QmgvCn{ED|>B2}$jfNBbYoa>tBcIqt3GHFkl!k3>XFs1BL;^fMLKe zU>Nv+80hJ&{V*=CC(2@7l&zj&xlr+3w3Wmp?&DizF(;iRCtL9x z+;yj!eyEtvmqb55K(vbY2mS1NgWcURbyKLmFYH#n|E(hUmH}J;^w5WR*!N)-=rG$h SZiu(mAXN>1y<66!2LA$?p0^GF literal 0 HcmV?d00001 diff --git a/nm-otool/tests/test_facile.c b/nm-otool/tests/test_facile.c new file mode 100644 index 00000000..aab6b7a6 --- /dev/null +++ b/nm-otool/tests/test_facile.c @@ -0,0 +1,7 @@ +#include + +int main(void) +{ + puts("Test facile"); + return (0); +} diff --git a/nm-otool/tests/test_moins_facile b/nm-otool/tests/test_moins_facile new file mode 100755 index 0000000000000000000000000000000000000000..54af825172c6b2483686ee86e26c92f9bc0a2943 GIT binary patch literal 8480 zcmeHMO=uHA6rR|sp{+JODD{`6SWyrC5ye^%Qf)BBqWu#@kRfTdZD8|5ve5=SR7=4s zg&uqMss}ykRg@}T^w^VlQ4|zBiHI1#Z+5obq@}&fJa{|v&AfU0=9?Xuo%ind_rE)Z z2-OKOvrP!m3>Djjm=hLjVjnaPm2#->V(fZsB~%SJMD())Ln+5%*T$+%)c8ns zo6s1sF&w3|kkYlMUDiCQ^*4kG&|jDCC$86ZRn&`_Ae6G`mXgKQrdoe9+TRXs;bVV5 zSo`z)J%4h_cJg)sMz#L#X@C2*17h;ND(m7KFS>zDutM?H=q;wLS`HNsY?Ze7i2;!hWes@7EWh&t&v@wd> z_*%6;|63#8iZ^dx4Gvts6zd=G_k>l$8!*MvlVe=LdA4ESb}0jIE@4ZER$?udg}I^L zg))kgF|mKz^7hmUO2*L$9fAgh^QUe$k2hA${?ZP1qePczf_9dQg-$1(>`axN6forg zlyN-Gef+R(r1$QlxwFf+m!G{jf;Pr08XJ_J@G;vU$1q=xU>GnA7zPXjh5^HXVZbo36$4k} z^Pl3g-y-paa~ELyI`)Sn9-MxR=M(W|7mDtm^RZv^KjO2mA~#-E+V9gq3@#ihqv#v0 zw5L%nPO=v!IEcRo{dkTq~6izWY&3> zZ^Fok8VykWE1?>eU{BvTXB#mL7zPXjh5^HXVZbn87%&VN1`Gp+0mFb{;6GuYr7bed zskB6yoKwkgnM8gV$?M^QSQLdwOH}5OfOaQv1D;H>q^+u98#G+b>Fa8Vejt3zuRaIBcBHoV&>&gvZ|yT i&miXAhR(f4S+|g(T_2-VF(L)N7NbA8b< + +int une_globale = 40; + +int main(void) +{ + printf("La globale vaut: %d\n", une_globale); + return (0); +}