<div dir="ltr"><div class="gmail_default" style="font-family:times new roman,serif">re-format patch:<br><br>--<br>--- a/Elf.hs    2012-12-04 19:27:51.000000000 -0800<br>+++ b/Elf.hs    2014-05-14 23:47:08.565983310 -0700<br>
@@ -666,12 +666,12 @@ getSymbolTableEntries e s =<br> -- If the size is zero, or the offset larger than the 'elfSectionData',<br> -- then 'Nothing' is returned.<br> findSymbolDefinition :: ElfSymbolTableEntry -> Maybe B.ByteString<br>
-findSymbolDefinition e =<br>-    let enclosingData = fmap elfSectionData (steEnclosingSection e)<br>-        start = fromIntegral (steValue e)<br>+findSymbolDefinition e = steEnclosingSection e >>= \enclosingSection -><br>
+    let enclosingData = elfSectionData enclosingSection<br>+        start = ( (fromIntegral (steValue e)) - (fromIntegral (elfSectionAddr enclosingSection) ) )<br>         len = fromIntegral (steSize e)<br>-        def = fmap (B.take len . B.drop start) enclosingData<br>
-    in if def == Just B.empty then Nothing else def<br>+        def = (B.take len . B.drop start) enclosingData<br>+    in if def == B.empty then Nothing else Just def<br> <br> runGetMany :: Get a -> L.ByteString -> [a]<br>
 runGetMany g bs<br>@@ -712,7 +712,7 @@ getSymbolTableEntry e strtlb =<br>     return $ EST (nameIdx,name) sec typ bind other sTlbIdx symVal size<br> <br> sectionByIndex :: Elf -> ElfSectionIndex -> Maybe ElfSection<br>
-sectionByIndex e (SHNIndex i) = lookup i . zip [1..] $ (elfSections e)<br>+sectionByIndex e (SHNIndex i) = lookup i . zip [0..] $ (elfSections e)<br> sectionByIndex _ _ = Nothing<br> <br> infoToTypeAndBind :: Word8 -> (ElfSymbolType,ElfSymbolBinding)<br>
<br>--<br><br><a href="http://github.com/erikcharlebois/elf">http://github.com/erikcharlebois/elf</a> gives 404.<br><br></div><div class="gmail_default" style="font-family:times new roman,serif">Thanks<br></div><div class="gmail_default" style="font-family:times new roman,serif">
Baojun<br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Wed, May 14, 2014 at 11:18 PM, Baojun Wang <span dir="ltr"><<a href="mailto:wangbj@gmail.com" target="_blank">wangbj@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_default" style="font-family:times new roman,serif">Hi list,<br><br></div><div class="gmail_default" style="font-family:times new roman,serif">
with below test program:<br><br>--<br></div><div class="gmail_default" style="font-family:times new roman,serif">
​/* test1.c: build with -g -O0 */<br>include <stdio.h><br><br>static const char appmsg[] = "hello, world";<br><br>int main(int argc, char* argv[])<br>{<br>        fputs(appmsg, stdout);<br><br>        return 0;<br>

}<br><br></div><div class="gmail_default" style="font-family:times new roman,serif"><br></div><div class="gmail_default" style="font-family:times new roman,serif">--- elf-test1.hs<br></div><div class="gmail_default" style="font-family:times new roman,serif">

module Main where<br><br>import qualified Data.ByteString as B<br>import qualified Data.ByteString.Char8 as C<br><br>import Control.Monad<br><br>import Data.Elf<br><br>testelf = "/tmp/test1"<br>testelfsym = C.pack "appmsg"<br>

<br>lookupSymbol1 _ [] = Nothing<br>lookupSymbol1 sym (t:ts) =<br>   case (snd (steName t)) of<br>     Nothing -> lookupSymbol1 sym ts<br>     Just sname -> if sname == sym then Just t<br>                   else lookupSymbol1 sym ts<br>

<br>lookupSymbol _ [] = Nothing<br>lookupSymbol sym (t:ts) =<br>  case (lookupSymbol1 sym t) of<br>    Nothing -> lookupSymbol sym ts<br>    t1 -> t1<br><br>test1 elf symtab symbol = mapM_ (print) (elfSections elf)<br>

<br>test2 elf symtab symbol =<br>  lookupSymbol symbol symtab<br><br>test3 elf symtab symbol =<br>  lookupSymbol symbol symtab >>= \et -><br>  findSymbolDefinition et<br><br>mainloop elf symtab symbol =<br>--  (test1 elf symtab symbol) >><br>

  print (test2 elf symtab symbol) >><br>  print (test3 elf symtab symbol) >>  <br>  return ()<br><br>main = do<br>  contents <- B.readFile testelf<br>  let elf = parseElf contents<br>      symtab = parseSymbolTables elf<br>

  mainloop elf symtab testelfsym<br>---<br><br></div><div class="gmail_default" style="font-family:times new roman,serif">the latest Data.Elf doesn't geive correct output as expected:<br><br></div><div class="gmail_default" style="font-family:times new roman,serif">

output will be:<br><br>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})<br>

<br></div><div class="gmail_default" style="font-family:times new roman,serif">From above, you can see the steEnclosingSection is wrong and offset by 1.<br><br></div><div class="gmail_default" style="font-family:times new roman,serif">

The *correct* output should be:<br>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})<br>

Just "hello, world\NUL"<br><br></div><div class="gmail_default" style="font-family:times new roman,serif">After check Elf.hs, I found there could be two issues:<br></div><div class="gmail_default" style="font-family:times new roman,serif">

1) in sectionByIndex, (SHNIndex) should start from 0, not 1; this cause the steEnclosingSection from my exmaple offset by 1;<br></div><div class="gmail_default" style="font-family:times new roman,serif">2) in findSymbolDefinition, start should substract the sectionAddr (base address).<br>

</div><div class="gmail_default" style="font-family:times new roman,serif"></div><div class="gmail_default" style="font-family:times new roman,serif"><br>please see below diff for details:<br><br>--<br><br>--- a/Elf.hs   2012-12-04 19:27:51.000000000 -0800<br>

+++ b/Elf.hs      2014-05-14 22:57:01.014498515 -0700<br>@@ -1,5 +1,5 @@<br> -- | Data.Elf  is a module for parsing a ByteString of an ELF file into an Elf record.<br>-module Data.Elf ( parseElf<br>+module Elf ( parseElf<br>

                 , parseSymbolTables<br>                 , findSymbolDefinition<br>                 , Elf(..)<br>@@ -666,12 +666,12 @@ getSymbolTableEntries e s =<br> -- If the size is zero, or the offset larger than the 'elfSectionData',<br>

 -- then 'Nothing' is returned.<br> findSymbolDefinition :: ElfSymbolTableEntry -> Maybe B.ByteString<br>-findSymbolDefinition e =<br>-    let enclosingData = fmap elfSectionData (steEnclosingSection e)<br>-        start = fromIntegral (steValue e)<br>

+findSymbolDefinition e = steEnclosingSection e >>= \enclosingSection -><br>+    let enclosingData = elfSectionData enclosingSection<br>+        start = ( (fromIntegral (steValue e)) - (fromIntegral (elfSectionAddr enclosingSection) ) )<br>

         len = fromIntegral (steSize e)<br>-        def = fmap (B.take len . B.drop start) enclosingData<br>-    in if def == Just B.empty then Nothing else def<br>+        def = (B.take len . B.drop start) enclosingData<br>

+    in if def == B.empty then Nothing else Just def<br> <br> runGetMany :: Get a -> L.ByteString -> [a]<br> runGetMany g bs<br>@@ -712,7 +712,7 @@ getSymbolTableEntry e strtlb =<br>     return $ EST (nameIdx,name) sec typ bind other sTlbIdx symVal size<br>

 <br> sectionByIndex :: Elf -> ElfSectionIndex -> Maybe ElfSection<br>-sectionByIndex e (SHNIndex i) = lookup i . zip [1..] $ (elfSections e)<br>+sectionByIndex e (SHNIndex i) = lookup i . zip [0..] $ (elfSections e)<br>

 sectionByIndex _ _ = Nothing<br> <br> infoToTypeAndBind :: Word8 -> (ElfSymbolType,ElfSymbolBinding)<br><br>--<br><br></div><div class="gmail_default" style="font-family:times new roman,serif">Would you please check above change and update Data.Elf hackage? I cannot find the author from <a href="http://hackage.haskell.org/package/elf-0.27" target="_blank">http://hackage.haskell.org/package/elf-0.27</a><br>

</div><div class="gmail_default" style="font-family:times new roman,serif"><br></div><div class="gmail_default" style="font-family:times new roman,serif">Thanks<span class="HOEnZb"><font color="#888888"><br>baojun<br></font></span></div>
</div>
</blockquote></div><br></div>