Releases: wangbj/elf
parse symbols from dynsym
v0.29
v0.28 (bug fixes)
Hi list,
with below test program:
/* test1.c: build with -g -O0 */
include <stdio.h>
static const char appmsg[] = "hello, world";
int main(int argc, char* argv[])
{
fputs(appmsg, stdout);
return 0;
}
--- elf-test1.hs
module Main where
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Control.Monad
import Data.Elf
testelf = "/tmp/test1"
testelfsym = C.pack "appmsg"
lookupSymbol1 _ [] = Nothing
lookupSymbol1 sym (t:ts) =
case (snd (steName t)) of
Nothing -> lookupSymbol1 sym ts
Just sname -> if sname == sym then Just t
else lookupSymbol1 sym ts
lookupSymbol _ [] = Nothing
lookupSymbol sym (t:ts) =
case (lookupSymbol1 sym t) of
Nothing -> lookupSymbol sym ts
t1 -> t1
test1 elf symtab symbol = mapM_ (print) (elfSections elf)
test2 elf symtab symbol =
lookupSymbol symbol symtab
test3 elf symtab symbol =
lookupSymbol symbol symtab >>= \et ->
findSymbolDefinition et
mainloop elf symtab symbol =
-- (test1 elf symtab symbol) >>
print (test2 elf symtab symbol) >>
print (test3 elf symtab symbol) >>
return ()
main = do
contents <- B.readFile testelf
let elf = parseElf contents
symtab = parseSymbolTables elf
mainloop elf symtab testelfsym
the latest Data.Elf doesn't geive correct output as expected:
output will be:
Just (EST {steName = (9,Just "appmsg"), steEnclosingSection = Just (ElfSection {elfSectionName = ".fini", elfSectionType = SHT_PROGBITS, elfSectionFlags = [SHF_EXECINSTR,SHF_ALLOC], elfSectionAddr = 4195908, elfSectionSize = 9, elfSectionLink = 0, elfSectionInfo = 0, elfSectionAddrAlign = 4, elfSectionEntSize = 0, elfSectionData = "H\131\236\bH\131\196\b\195"}), steType = STTObject, steBind = STBLocal, steOther = 0, steIndex = SHNIndex 14, steValue = 4195924, steSize = 13})
From above, you can see the steEnclosingSection is wrong and offset by 1.
The correct output should be:
Just (EST {steName = (9,Just "appmsg"), steEnclosingSection = Just (ElfSection {elfSectionName = ".rodata", elfSectionType = SHT_PROGBITS, elfSectionFlags = [SHF_ALLOC], elfSectionAddr = 4195920, elfSectionSize = 17, elfSectionLink = 0, elfSectionInfo = 0, elfSectionAddrAlign = 4, elfSectionEntSize = 0, elfSectionData = "\SOH\NUL\STX\NULhello, world\NUL"}), steType = STTObject, steBind = STBLocal, steOther = 0, steIndex = SHNIndex 14, steValue = 4195924, steSize = 13})
Just "hello, world\NUL"
After check Elf.hs, I found there could be two issues:
- in sectionByIndex, (SHNIndex) should start from 0, not 1; this cause the steEnclosingSection from my exmaple offset by 1;
- in findSymbolDefinition, start should substract the sectionAddr (base address).
v0.27 cloned from hackage
This release is exactly the same as v0.27 from hackage (https://hackage.haskell.org/package/elf).
The package is no longer exist in github (by Erik Charlebois), thus (re-)creating this repo for maintaining purpose.