elf v0.28 Release Notes

Release Date: 2017-02-23 // almost 6 years ago
  • 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:

    1. in sectionByIndex, (SHNIndex) should start from 0, not 1; this cause the steEnclosingSection from my exmaple offset by 1;
    2. in findSymbolDefinition, start should substract the sectionAddr (base address).