]> gitweb.factorcode.org Git - factor.git/commitdiff
nm for ELF files
authorErik Charlebois <erikcharlebois@gmail.com>
Tue, 13 Apr 2010 02:10:20 +0000 (19:10 -0700)
committerErik Charlebois <erikcharlebois@gmail.com>
Tue, 13 Apr 2010 02:10:20 +0000 (19:10 -0700)
extra/elf/elf.factor
extra/elf/nm/authors.txt [new file with mode: 0644]
extra/elf/nm/nm-docs.factor [new file with mode: 0644]
extra/elf/nm/nm.factor [new file with mode: 0644]
extra/elf/nm/summary.txt [new file with mode: 0644]

index bf4de754d14004f0e4676aaf31e9f7493b7cd87e..b2fe7db8a4ee52e56591294fc0b91597e1b559c2 100644 (file)
@@ -1,8 +1,8 @@
 ! 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 io.encodings.ascii kernel locals math math.intervals
-sequences specialized-arrays strings typed ;
+classes.struct fry io.encodings.ascii kernel locals math
+math.intervals sequences specialized-arrays strings typed ;
 IN: elf
 
 ! FFI data
@@ -609,4 +609,6 @@ M:: segment sections ( segment -- sections )
     symbol sym>> st_value>> segment p_vaddr>> - segment p_offset>> + :> faddress
     faddress symbol elf-header>> >c-ptr <displaced-alien>
     symbol sym>> st_size>> <direct-uchar-array> ;
-    
+
+: find-section ( sections name -- section/f )
+    '[ name>> _ = ] find nip ;
diff --git a/extra/elf/nm/authors.txt b/extra/elf/nm/authors.txt
new file mode 100644 (file)
index 0000000..6f03a12
--- /dev/null
@@ -0,0 +1 @@
+Erik Charlebois
diff --git a/extra/elf/nm/nm-docs.factor b/extra/elf/nm/nm-docs.factor
new file mode 100644 (file)
index 0000000..f07af89
--- /dev/null
@@ -0,0 +1,22 @@
+! Copyright (C) 2010 Erik Charlebois.
+! See http://factorcode.org/license.txt for BSD license.
+USING: elf help.markup help.syntax ;
+IN: elf.nm
+
+HELP: nm
+{ $values
+    { "path" "a pathname string" }
+}
+{ $description "Prints information about the symbols in the ELF object at the given path." } ;
+
+HELP: print-symbol
+{ $values
+    { "sections" "sequence of section" } { "symbol" 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." }
+;
+
+ABOUT: "elf.nm"
diff --git a/extra/elf/nm/nm.factor b/extra/elf/nm/nm.factor
new file mode 100644 (file)
index 0000000..f9df612
--- /dev/null
@@ -0,0 +1,25 @@
+! Copyright (C) 2010 Erik Charlebois.
+! See http://factorcode.org/license.txt for BSD license.
+USING: accessors combinators elf formatting io.mmap kernel sequences ;
+IN: elf.nm
+
+: print-symbol ( sections symbol -- )
+    [ sym>> st_value>> "%016d " printf ]
+    [
+        sym>> st_shndx>>
+        {
+            { SHN_UNDEF [ drop "undefined" ] }
+            { SHN_ABS [ drop "absolute" ] }
+            { SHN_COMMON [ drop "common" ] }
+            [ swap nth name>> ]
+        } case "%-16s " printf
+    ]
+    [ name>> "%s\n" printf ] tri ;
+    
+: nm ( path -- )
+    [
+        address>> <elf> sections
+        dup ".symtab" find-section
+        symbols [ name>> empty? not ] filter
+        [ print-symbol ] with each
+    ] with-mapped-file ;
diff --git a/extra/elf/nm/summary.txt b/extra/elf/nm/summary.txt
new file mode 100644 (file)
index 0000000..7d378d3
--- /dev/null
@@ -0,0 +1 @@
+UNIX nm-like utility.