Skip to content

Commit

Permalink
dump: Add a command to print out the Data.Macaw.Memory.Memory
Browse files Browse the repository at this point in the history
  • Loading branch information
langston-barrett committed Dec 19, 2024
1 parent 19d9e9e commit c93c152
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 0 deletions.
1 change: 1 addition & 0 deletions macaw-aarch32-symbolic/macaw-aarch32-symbolic.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ executable macaw-aarch32-dump
macaw-aarch32,
macaw-aarch32-symbolic,
macaw-dump,
macaw-loader-aarch32,
macaw-symbolic

test-suite macaw-aarch32-symbolic-tests
Expand Down
1 change: 1 addition & 0 deletions macaw-aarch32-symbolic/tools/Dump.hs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ module Main (main) where
import Data.Macaw.ARM.ARMReg ()
import Data.Macaw.AArch32.Symbolic ()
import Data.Macaw.ARM qualified as MA
import Data.Macaw.BinaryLoader.AArch32 ()
import Data.Macaw.Dump qualified as MD
import Data.Macaw.Symbolic qualified as MS
import Data.Proxy (Proxy(..))
Expand Down
2 changes: 2 additions & 0 deletions macaw-dump/macaw-dump.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ library
elf-edit,
lens,
macaw-base,
macaw-loader,
macaw-symbolic,
optparse-applicative,
parameterized-utils,
Expand All @@ -103,4 +104,5 @@ library
Data.Macaw.Dump.CLI
Data.Macaw.Dump.CLIUtils
Data.Macaw.Dump.Discover
Data.Macaw.Dump.Memory
Data.Macaw.Dump.Plt
4 changes: 4 additions & 0 deletions macaw-dump/src/Data/Macaw/Dump.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,21 @@ import Data.ElfEdit qualified as EE
import Data.Macaw.Architecture.Info qualified as MAI
import Data.Macaw.Dump.CLI qualified as MDC
import Data.Macaw.Dump.Discover qualified as MDD
import Data.Macaw.Dump.Memory qualified as MDM
import Data.Macaw.Dump.Plt qualified as MDP
import Data.Macaw.CFG qualified as MC
import Data.Macaw.Memory.ElfLoader.PLTStubs qualified as MMEP
import Data.Macaw.Symbolic qualified as MS
import Lang.Crucible.CFG.Extension qualified as CCE
import Data.Macaw.BinaryLoader qualified as Loader

main ::
( MS.GenArchInfo MS.LLVMMemory arch
, CCE.IsSyntaxExtension (MS.MacawExt arch)
, MC.ArchConstraints arch
, MC.ArchAddrWidth arch ~ EE.RelocationWidth reloc
, EE.IsRelocationType reloc
, Loader.BinaryLoader arch (EE.ElfHeaderInfo (MC.ArchAddrWidth arch))
) =>
MAI.ArchitectureInfo arch ->
MS.GenArchVals mem arch ->
Expand All @@ -32,4 +35,5 @@ main archInfo archVals pltStubInfo = do
cli <- MDC.parseCli
case MDC.cliCommand cli of
MDC.CommandDiscover cfg -> MDD.discover archInfo archVals cfg
MDC.CommandMemory cfg -> MDM.memory archInfo cfg
MDC.CommandPlt cfg -> MDP.plt archInfo pltStubInfo cfg
9 changes: 9 additions & 0 deletions macaw-dump/src/Data/Macaw/Dump/CLI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,21 @@ module Data.Macaw.Dump.CLI

import Control.Applicative ((<**>))
import Data.Macaw.Dump.Discover qualified as MDD
import Data.Macaw.Dump.Memory qualified as MDM
import Data.Macaw.Dump.Plt qualified as MDP
import Options.Applicative qualified as Opt

data Command
= CommandDiscover MDD.DiscoverConfig
| CommandMemory MDM.MemoryConfig
| CommandPlt MDP.PltConfig

command :: Opt.Parser Command
command =
Opt.subparser $
mconcat
[ cmdDiscover
, cmdMemory
, cmdPlt
]
where
Expand All @@ -31,6 +34,12 @@ command =
"discover"
(Opt.info (CommandDiscover <$> MDD.discoverConfig) (Opt.progDesc "Perform code discovery and print CFGs"))

cmdMemory :: Opt.Mod Opt.CommandFields Command
cmdMemory = do
Opt.command
"memory"
(Opt.info (CommandMemory <$> MDM.memoryConfig) (Opt.progDesc "Print program memory"))

cmdPlt :: Opt.Mod Opt.CommandFields Command
cmdPlt = do
Opt.command
Expand Down
51 changes: 51 additions & 0 deletions macaw-dump/src/Data/Macaw/Dump/Memory.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
{-# LANGUAGE ImportQualifiedPost #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE FlexibleContexts #-}

module Data.Macaw.Dump.Memory
( MemoryConfig(..)
, memoryConfig
, memory
) where

import Data.ElfEdit qualified as EE
import Data.Macaw.Architecture.Info qualified as MAI
import Data.Macaw.BinaryLoader qualified as Loader
import Data.Macaw.CFG.Core qualified as MC
import Data.Macaw.Dump.CLIUtils qualified as MDCU
import Data.Macaw.Memory.LoadCommon qualified as LC
import Data.Macaw.Memory qualified as MM
import Options.Applicative qualified as Opt

data MemoryConfig
= MemoryConfig
{ memBinPath :: FilePath
}

memoryConfig :: Opt.Parser MemoryConfig
memoryConfig =
MemoryConfig
<$> MDCU.binOpt

-- Currently, we do not apply any offsets to addresses in the loaded binary. We
-- will need to reconsider this if we want to support shared libraries.
loadOptions :: LC.LoadOptions
loadOptions =
LC.LoadOptions
{ LC.loadOffset = Nothing
}

memory ::
forall arch.
( MM.MemWidth (MC.ArchAddrWidth arch)
, Loader.BinaryLoader arch (EE.ElfHeaderInfo (MC.ArchAddrWidth arch))
) =>
MAI.ArchitectureInfo arch ->
MemoryConfig ->
IO ()
memory archInfo cfg = do
ehi <- MDCU.loadElf archInfo (memBinPath cfg)
loaded <- Loader.loadBinary @arch loadOptions ehi
let mem = Loader.memoryImage loaded
print mem
1 change: 1 addition & 0 deletions macaw-ppc-symbolic/macaw-ppc-symbolic.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ executable macaw-ppc-dump
elf-edit,
macaw-base,
macaw-dump,
macaw-loader-ppc,
macaw-ppc,
macaw-ppc-symbolic,
macaw-symbolic
Expand Down
1 change: 1 addition & 0 deletions macaw-ppc-symbolic/tools/Dump.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Main (main) where

import Data.ElfEdit qualified as EE
import Data.Macaw.BinaryLoader.PPC ()
import Data.Macaw.Dump qualified as MD
import Data.Macaw.Memory.ElfLoader.PLTStubs qualified as MMEP
import Data.Macaw.PPC qualified as PPC
Expand Down
1 change: 1 addition & 0 deletions x86_symbolic/macaw-x86-symbolic.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ executable macaw-x86-dump
base,
containers,
macaw-x86,
macaw-loader-x86,
macaw-x86-symbolic,
macaw-dump,
macaw-symbolic
Expand Down
1 change: 1 addition & 0 deletions x86_symbolic/tools/Dump.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

module Main (main) where

import Data.Macaw.BinaryLoader.X86 ()
import Data.Macaw.Dump qualified as MD
import Data.Macaw.Symbolic qualified as MS
import Data.Macaw.X86 qualified as MX
Expand Down

0 comments on commit c93c152

Please sign in to comment.