! 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
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 ;
--- /dev/null
+Erik Charlebois
--- /dev/null
+! 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"
--- /dev/null
+! 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 ;
--- /dev/null
+UNIX nm-like utility.