From 904a2057282cb31af9511173cfa8f1f03d939e6a Mon Sep 17 00:00:00 2001 From: Erik Charlebois Date: Mon, 12 Apr 2010 21:23:26 -0700 Subject: [PATCH] Test cases for ELF --- extra/elf/elf-tests.factor | 180 +++++++++++++++++++++++++++++++++++ extra/elf/elf.factor | 16 +++- extra/elf/nm/nm-docs.factor | 2 +- extra/elf/nm/nm-tests.factor | 51 ++++++++++ extra/elf/nm/nm.factor | 5 +- 5 files changed, 248 insertions(+), 6 deletions(-) create mode 100644 extra/elf/elf-tests.factor create mode 100644 extra/elf/nm/nm-tests.factor diff --git a/extra/elf/elf-tests.factor b/extra/elf/elf-tests.factor new file mode 100644 index 0000000000..c0ade1bcd1 --- /dev/null +++ b/extra/elf/elf-tests.factor @@ -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 diff --git a/extra/elf/elf.factor b/extra/elf/elf.factor index b2fe7db8a4..19bb3bfbf9 100644 --- a/extra/elf/elf.factor +++ b/extra/elf/elf.factor @@ -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>> ; : 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>> @ + ] with-mapped-file ; inline diff --git a/extra/elf/nm/nm-docs.factor b/extra/elf/nm/nm-docs.factor index f07af890c8..a7b7ad426e 100644 --- a/extra/elf/nm/nm-docs.factor +++ b/extra/elf/nm/nm-docs.factor @@ -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 index 0000000000..e420976d9e --- /dev/null +++ b/extra/elf/nm/nm-tests.factor @@ -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 } +[ dup [ "resource:extra/elf/a.out" nm ] with-output-stream >string ] +unit-test diff --git a/extra/elf/nm/nm.factor b/extra/elf/nm/nm.factor index f9df61249d..87c9abf00b 100644 --- a/extra/elf/nm/nm.factor +++ b/extra/elf/nm/nm.factor @@ -18,8 +18,7 @@ IN: elf.nm : nm ( path -- ) [ - address>> 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 ;