Skip to content

Commit

Permalink
dump: Allow specifying the load offset via the CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
langston-barrett committed Dec 19, 2024
1 parent 3e1e3ea commit d44b183
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions macaw-dump/src/Data/Macaw/Dump/Memory.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,25 +16,33 @@ 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 Data.Word (Word64)
import Options.Applicative qualified as Opt

data MemoryConfig
= MemoryConfig
{ memBinPath :: FilePath
{ memLoadOffset :: Maybe Word64
, memBinPath :: FilePath
}

loadOffsetOpt :: Opt.Parser Word64
loadOffsetOpt = Opt.option Opt.auto opts
where
opts =
mconcat
[ Opt.long "offset"
, Opt.help "base offset at which to load the file"
, Opt.showDefault
]

memoryConfig :: Opt.Parser MemoryConfig
memoryConfig =
MemoryConfig
<$> MDCU.binOpt
<$> Opt.optional loadOffsetOpt
<*> 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
}
loadOptions :: MemoryConfig -> LC.LoadOptions
loadOptions cfg = LC.LoadOptions { LC.loadOffset = memLoadOffset cfg }

memory ::
forall arch.
Expand All @@ -46,6 +54,7 @@ memory ::
IO ()
memory archInfo cfg = do
ehi <- MDCU.loadElf archInfo (memBinPath cfg)
loaded <- Loader.loadBinary @arch loadOptions ehi
let loadOpts = loadOptions cfg
loaded <- Loader.loadBinary @arch loadOpts ehi
let mem = Loader.memoryImage loaded
print mem

0 comments on commit d44b183

Please sign in to comment.