From d8c26b7d876a29bf3a0cb697a60e9a38e93ab6e5 Mon Sep 17 00:00:00 2001 From: Erik Charlebois Date: Thu, 15 Apr 2010 01:20:57 -0700 Subject: [PATCH 1/2] Fat binary loading --- extra/macho/a2.macho | Bin 0 -> 12888 bytes extra/macho/macho-tests.factor | 10 ++++++++-- extra/macho/macho.factor | 31 ++++++++++++++++++++++++++----- 3 files changed, 34 insertions(+), 7 deletions(-) create mode 100755 extra/macho/a2.macho diff --git a/extra/macho/a2.macho b/extra/macho/a2.macho new file mode 100755 index 0000000000000000000000000000000000000000..ed9a3a9a278bdaf8c3236ad2b96012df6d46c7dc GIT binary patch literal 12888 zcmeHN-)me&6h1dun^co@+Xs!<=vG=9Y)L9YsbFCv4cX9A*Cs7NlyNtElU=#lUGCmZ zH$rhCC_g-C-8+-Ln{CqCQA{E3J-jz~{aq+28j@yQuveMpup0u}*_fJML}U=gqgSOhEr76FTZ zMZh9p5%?bF#1@EzQj zXlED$C^bDkGu_@j)Ti4aPFlv?@YXg+qsAGP! zwZJcx=XIx@>rHEW{kolUoZ8lo2Bqrd%cXKbmCAFKP;Q@JR@_+4g7oB8#F_}P8=)lXIxgw%k5@wP|wdyi*j7woMSVbQdPe)58TvZ&aZW@H?8dr zYX!=D4(>%t6>>qY)w&bEqPCaPT`0%*p`{s>TF?Et$@6E&&rF(U76#h(0WxVuFP7^J z+rwIT67gO+q01pl;r$YmPeI0?%phxk(+v0IJIFlKpB@r<4;<%2vPdNMVd7YOk<6Z> z*g+papZ#_cUJK#4e6C09HGkAA?JBat+iwQwV*J2?AXZ35#UhDvy=XR0B-agxdO>) z)0v@&b<%;hW9%M7rG|WFtj4FIUo-2o1p!)NpfD#UJ$2|-1Gh=NXfpQ-Zc&> z$B{VBFy=&hgq0*NLP4Q_qLOo@79+inEOQ)>!aMzys7za`nJs? zU=gqgSOhEr76FTZMZh9p5wHkY1S|p;fqxf){b{Ate50WduC7(eIj dup [ "resource:extra/macho/a.macho" macho-nm ] with-output-stream >string ] unit-test + +{ t } [ + "resource:extra/macho/a2.macho" [ + >c-ptr fat-binary-members first data>> >c-ptr macho-header 64-bit? + ] with-mapped-macho +] unit-test diff --git a/extra/macho/macho.factor b/extra/macho/macho.factor index 79cb59c148..70dc594e07 100644 --- a/extra/macho/macho.factor +++ b/extra/macho/macho.factor @@ -4,7 +4,7 @@ USING: accessors alien alien.c-types alien.strings alien.syntax classes classes.struct combinators combinators.short-circuit io.encodings.ascii io.encodings.string kernel literals make math sequences specialized-arrays typed fry io.mmap formatting -locals splitting ; +locals splitting io.binary arrays ; FROM: alien.c-types => short ; IN: macho @@ -812,7 +812,7 @@ C-ENUM: reloc_type_ppc PPC_RELOC_LOCAL_SECTDIFF ; ! Low-level interface -SPECIALIZED-ARRAYS: section section_64 nlist nlist_64 ; +SPECIALIZED-ARRAYS: section section_64 nlist nlist_64 fat_arch uchar ; UNION: mach_header_32/64 mach_header mach_header_64 ; UNION: segment_command_32/64 segment_command segment_command_64 ; UNION: load-command segment_command segment_command_64 @@ -826,6 +826,26 @@ UNION: section_32/64-array section-array section_64-array ; UNION: nlist_32/64 nlist nlist_64 ; UNION: nlist_32/64-array nlist-array nlist_64-array ; +TUPLE: fat-binary-member cpu-type cpu-subtype data ; +ERROR: not-fat-binary ; + +TYPED: fat-binary-members ( >c-ptr -- fat-binary-members ) + fat_header memory>struct dup magic>> { + { FAT_MAGIC [ ] } + { FAT_CIGAM [ ] } + [ 2drop not-fat-binary ] + } case dup + [ >c-ptr fat_header heap-size swap ] + [ nfat_arch>> 4 >be le> ] bi + [ + { + [ nip cputype>> 4 >be le> ] + [ nip cpusubtype>> 4 >be le> ] + [ offset>> 4 >be le> swap >c-ptr ] + [ nip size>> 4 >be le> ] + } 2cleave fat-binary-member boa + ] with { } map-as ; + TYPED: 64-bit? ( macho: mach_header_32/64 -- ? ) magic>> { { MH_MAGIC_64 [ t ] } @@ -924,12 +944,13 @@ TYPED: load-commands ( macho: mach_header_32/64 -- load-commands ) : macho-nm ( path -- ) [| macho | macho load-commands segment-commands sections-array :> sections - macho load-commands symtab-commands [| symtab | macho symtab symbols [ [ drop n_value>> "%016x " printf ] - [ drop n_sect>> sections nth sectname>> - read-array-string "%-16s" printf ] + [ + drop n_sect>> sections nth sectname>> + read-array-string "%-16s" printf + ] [ symbol-name "%s\n" printf ] 2tri ] curry each ] each From ccda46921f446e74c56adac03013925f115cf224 Mon Sep 17 00:00:00 2001 From: Erik Charlebois Date: Thu, 15 Apr 2010 16:31:37 -0700 Subject: [PATCH 2/2] Don't run mach-o and elf tests on ppc until endian issue sorted out --- extra/elf/elf-tests.factor | 4 +++- extra/elf/nm/nm-tests.factor | 12 +++++++----- extra/macho/macho-tests.factor | 22 ++++++++++++---------- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/extra/elf/elf-tests.factor b/extra/elf/elf-tests.factor index d68885e6b7..4d1bb5be06 100644 --- a/extra/elf/elf-tests.factor +++ b/extra/elf/elf-tests.factor @@ -1,8 +1,9 @@ ! Copyright (C) 2010 Erik Charlebois. ! See http://factorcode.org/license.txt for BSD license. -USING: accessors byte-arrays elf kernel sequences tools.test ; +USING: accessors byte-arrays elf kernel sequences system tools.test ; IN: elf.tests +cpu ppc? [ { { "" @@ -178,3 +179,4 @@ unit-test ] with-mapped-elf ] unit-test +] unless diff --git a/extra/elf/nm/nm-tests.factor b/extra/elf/nm/nm-tests.factor index 9e529ae43d..90d9634750 100644 --- a/extra/elf/nm/nm-tests.factor +++ b/extra/elf/nm/nm-tests.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2010 Erik Charlebois. ! See http://factorcode.org/license.txt for BSD license. -USING: elf.nm io io.streams.string kernel multiline strings tools.test -literals ; +USING: elf.nm io io.streams.string kernel literals multiline strings +system tools.test ; IN: elf.nm.tests STRING: validation-output @@ -46,6 +46,8 @@ STRING: validation-output ; -{ $ validation-output } -[ dup [ "resource:extra/elf/a.elf" elf-nm ] with-output-stream >string ] -unit-test +cpu ppc? [ + { $ validation-output } + [ dup [ "resource:extra/elf/a.elf" elf-nm ] with-output-stream >string ] + unit-test +] unless diff --git a/extra/macho/macho-tests.factor b/extra/macho/macho-tests.factor index d52eb778f6..561a98cd70 100644 --- a/extra/macho/macho-tests.factor +++ b/extra/macho/macho-tests.factor @@ -1,7 +1,7 @@ ! Copyright (C) 2010 Erik Charlebois. ! See http://factorcode.org/license.txt for BSD license. USING: accessors alien io io.streams.string kernel literals macho -multiline sequences strings tools.test ; +multiline sequences strings system tools.test ; IN: macho.tests STRING: validation-output @@ -21,12 +21,14 @@ STRING: validation-output ; -{ $ validation-output } -[ dup [ "resource:extra/macho/a.macho" macho-nm ] with-output-stream >string ] -unit-test - -{ t } [ - "resource:extra/macho/a2.macho" [ - >c-ptr fat-binary-members first data>> >c-ptr macho-header 64-bit? - ] with-mapped-macho -] unit-test +cpu ppc? [ + { $ validation-output } + [ dup [ "resource:extra/macho/a.macho" macho-nm ] with-output-stream >string ] + unit-test + + { t } [ + "resource:extra/macho/a2.macho" [ + >c-ptr fat-binary-members first data>> >c-ptr macho-header 64-bit? + ] with-mapped-macho + ] unit-test +] unless