]> gitweb.factorcode.org Git - factor.git/commitdiff
Test cases for ELF
authorErik Charlebois <erikcharlebois@gmail.com>
Tue, 13 Apr 2010 04:23:26 +0000 (21:23 -0700)
committerErik Charlebois <erikcharlebois@gmail.com>
Tue, 13 Apr 2010 04:23:26 +0000 (21:23 -0700)
extra/elf/elf-tests.factor [new file with mode: 0644]
extra/elf/elf.factor
extra/elf/nm/nm-docs.factor
extra/elf/nm/nm-tests.factor [new file with mode: 0644]
extra/elf/nm/nm.factor

diff --git a/extra/elf/elf-tests.factor b/extra/elf/elf-tests.factor
new file mode 100644 (file)
index 0000000..c0ade1b
--- /dev/null
@@ -0,0 +1,180 @@
+! Copyright (C) 2010 Erik Charlebois.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors byte-arrays elf kernel sequences tools.test ;
+IN: elf.tests
+
+{
+    {
+        ""
+        ".interp"
+        ".note.ABI-tag"
+        ".note.gnu.build-id"
+        ".hash"
+        ".gnu.hash"
+        ".dynsym"
+        ".dynstr"
+        ".gnu.version"
+        ".gnu.version_r"
+        ".rela.dyn"
+        ".rela.plt"
+        ".init"
+        ".plt"
+        ".text"
+        ".fini"
+        ".rodata"
+        ".eh_frame_hdr"
+        ".eh_frame"
+        ".ctors"
+        ".dtors"
+        ".jcr"
+        ".dynamic"
+        ".got"
+        ".got.plt"
+        ".data"
+        ".bss"
+        ".comment"
+        ".debug_aranges"
+        ".debug_pubnames"
+        ".debug_info"
+        ".debug_abbrev"
+        ".debug_line"
+        ".debug_str"
+        ".shstrtab"
+        ".symtab"
+        ".strtab"
+    }
+}
+[
+    "resource:extra/elf/a.out" [
+        sections [ name>> ] map
+    ] with-mapped-elf
+]
+unit-test
+
+{
+    {
+        ".interp"
+        ".note.ABI-tag"
+        ".note.gnu.build-id"
+        ".hash"
+        ".gnu.hash"
+        ".dynsym"
+        ".dynstr"
+        ".gnu.version"
+        ".gnu.version_r"
+        ".rela.dyn"
+        ".rela.plt"
+        ".init"
+        ".plt"
+        ".text"
+        ".fini"
+        ".rodata"
+        ".eh_frame_hdr"
+        ".eh_frame"
+    }
+}
+[
+    "resource:extra/elf/a.out" [
+        segments [ program-header>> p_type>> PT_LOAD = ] find nip
+        sections [ name>> ] map
+    ] with-mapped-elf
+]
+unit-test
+
+{
+    {
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        ""
+        "init.c"
+        "call_gmon_start"
+        "crtstuff.c"
+        "__CTOR_LIST__"
+        "__DTOR_LIST__"
+        "__JCR_LIST__"
+        "__do_global_dtors_aux"
+        "completed.7342"
+        "dtor_idx.7344"
+        "frame_dummy"
+        "crtstuff.c"
+        "__CTOR_END__"
+        "__FRAME_END__"
+        "__JCR_END__"
+        "__do_global_ctors_aux"
+        "test.c"
+        "_GLOBAL_OFFSET_TABLE_"
+        "__init_array_end"
+        "__init_array_start"
+        "_DYNAMIC"
+        "data_start"
+        "printf@@GLIBC_2.2.5"
+        "__libc_csu_fini"
+        "_start"
+        "__gmon_start__"
+        "_Jv_RegisterClasses"
+        "_fini"
+        "__libc_start_main@@GLIBC_2.2.5"
+        "_IO_stdin_used"
+        "__data_start"
+        "__dso_handle"
+        "__DTOR_END__"
+        "__libc_csu_init"
+        "__bss_start"
+        "_end"
+        "_edata"
+        "main"
+        "_init"
+    }
+}
+[
+    "resource:extra/elf/a.out" [
+        sections ".symtab" find-section symbols
+        [ name>> ] map
+    ] with-mapped-elf
+]
+unit-test
+
+{
+    B{
+        85 72 137 229 184 44 6 64 0 72 137 199 184 0 0 0 0 232 222
+        254 255 255 201 195
+    }
+}
+[
+    "resource:extra/elf/a.out" [
+        sections ".symtab" "main" find-section-symbol
+        symbol-data >byte-array
+    ] with-mapped-elf
+]
+unit-test
index b2fe7db8a4ee52e56591294fc0b91597e1b559c2..19bb3bfbf919a8e43319602ff5bf748bd62d7141 100644 (file)
@@ -1,7 +1,7 @@
 ! Copyright (C) 2010 Erik Charlebois.
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors alien alien.c-types alien.strings alien.syntax arrays
-classes.struct fry io.encodings.ascii kernel locals math
+classes.struct fry io.encodings.ascii io.mmap kernel locals math
 math.intervals sequences specialized-arrays strings typed ;
 IN: elf
 
@@ -611,4 +611,16 @@ M:: segment sections ( segment -- sections )
     symbol sym>> st_size>> <direct-uchar-array> ;
 
 : find-section ( sections name -- section/f )
-    '[ name>> _ = ] find nip ;
+    '[ name>> _ = ] find nip ; inline
+
+: find-symbol ( symbols name -- symbol/f )
+    '[ name>> _ = ] find nip ; inline
+
+: find-section-symbol ( sections section symbol -- symbol/f )
+    [ find-section ] dip over [
+        [ symbols ] dip find-symbol ] [ 2drop f ] if ;
+
+: with-mapped-elf ( path quot -- )
+    '[
+        address>> <elf> @
+    ] with-mapped-file ; inline
index f07af890c852486c84e553e5c33e013188c6a86b..a7b7ad426e1246d14e639bcde6ed0884ad69680e 100644 (file)
@@ -16,7 +16,7 @@ HELP: print-symbol
 { $description "Prints the value, section and name of the given symbol." } ;
 
 ARTICLE: "elf.nm" "ELF nm"
-{ $description "Utility to print the values, sections and names of the symbols in a given ELF file. In an ELF executable or shared library, the symbol values are typically their virtual addresses. In a relocatable ELF object, they are section-relative offsets." }
+"The " { $vocab-link "elf.nm" } " vocab prints the values, sections and names of the symbols in a given ELF file. In an ELF executable or shared library, the symbol values are typically their virtual addresses. In a relocatable ELF object, they are section-relative offsets."
 ;
 
 ABOUT: "elf.nm"
diff --git a/extra/elf/nm/nm-tests.factor b/extra/elf/nm/nm-tests.factor
new file mode 100644 (file)
index 0000000..e420976
--- /dev/null
@@ -0,0 +1,51 @@
+! 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 ;
+IN: elf.nm.tests
+
+STRING: validation-output
+0000000000000000 absolute         init.c
+0000000004195436 .text            call_gmon_start
+0000000000000000 absolute         crtstuff.c
+0000000006295064 .ctors           __CTOR_LIST__
+0000000006295080 .dtors           __DTOR_LIST__
+0000000006295096 .jcr             __JCR_LIST__
+0000000004195472 .text            __do_global_dtors_aux
+0000000006295584 .bss             completed.7342
+0000000006295592 .bss             dtor_idx.7344
+0000000004195584 .text            frame_dummy
+0000000000000000 absolute         crtstuff.c
+0000000006295072 .ctors           __CTOR_END__
+0000000004196056 .eh_frame        __FRAME_END__
+0000000006295096 .jcr             __JCR_END__
+0000000004195808 .text            __do_global_ctors_aux
+0000000000000000 absolute         test.c
+0000000006295528 .got.plt         _GLOBAL_OFFSET_TABLE_
+0000000006295060 .ctors           __init_array_end
+0000000006295060 .ctors           __init_array_start
+0000000006295104 .dynamic         _DYNAMIC
+0000000006295568 .data            data_start
+0000000000000000 undefined        printf@@GLIBC_2.2.5
+0000000004195648 .text            __libc_csu_fini
+0000000004195392 .text            _start
+0000000000000000 undefined        __gmon_start__
+0000000000000000 undefined        _Jv_RegisterClasses
+0000000004195864 .fini            _fini
+0000000000000000 undefined        __libc_start_main@@GLIBC_2.2.5
+0000000004195880 .rodata          _IO_stdin_used
+0000000006295568 .data            __data_start
+0000000006295576 .data            __dso_handle
+0000000006295088 .dtors           __DTOR_END__
+0000000004195664 .text            __libc_csu_init
+0000000006295584 absolute         __bss_start
+0000000006295600 absolute         _end
+0000000006295584 absolute         _edata
+0000000004195620 .text            main
+0000000004195312 .init            _init
+
+;
+
+{ $ validation-output }
+[ <string-writer> dup [ "resource:extra/elf/a.out" nm ] with-output-stream >string ]
+unit-test
index f9df61249df614d414a6103b1b54380d674d23db..87c9abf00bfbd60128855b3ec69bc2c2ccbcb3eb 100644 (file)
@@ -18,8 +18,7 @@ IN: elf.nm
     
 : nm ( path -- )
     [
-        address>> <elf> sections
-        dup ".symtab" find-section
+        sections dup ".symtab" find-section
         symbols [ name>> empty? not ] filter
         [ print-symbol ] with each
-    ] with-mapped-file ;
+    ] with-mapped-elf ;