From 7d8fc5e6688a9925f47933dd790c5a16f4381869 Mon Sep 17 00:00:00 2001 From: Daniel Matichuk Date: Wed, 10 May 2023 16:31:57 -0700 Subject: [PATCH] hack to prevent loading certain segments --- base/src/Data/Macaw/Memory/ElfLoader.hs | 22 +++++++++++++++++----- base/src/Data/Macaw/Memory/LoadCommon.hs | 5 +++-- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/base/src/Data/Macaw/Memory/ElfLoader.hs b/base/src/Data/Macaw/Memory/ElfLoader.hs index 55df33dc..d5862965 100644 --- a/base/src/Data/Macaw/Memory/ElfLoader.hs +++ b/base/src/Data/Macaw/Memory/ElfLoader.hs @@ -1182,7 +1182,6 @@ insertElfSegment regIdx addrOff shdrMap contents relocMap phdr = do mlsIndexMap %= Map.insert elfIdx addr _ -> error "Unexpected shdr interval" --- | Load an elf file into memory by parsing segments. memoryForElfSegments' :: forall w . RegionIndex @@ -1192,7 +1191,20 @@ memoryForElfSegments' , SectionIndexMap w -- Section index map , [MemLoadWarning] -- Warnings from load ) -memoryForElfSegments' regIndex addrOff elf = do +memoryForElfSegments' regIndex addrOff elf = memoryForElfSegments'' [] regIndex addrOff elf + +-- | Load an elf file into memory by parsing segments. +memoryForElfSegments'' + :: forall w + . [Int] + -> RegionIndex + -> Integer + -> Elf.ElfHeaderInfo w + -> Either String (Memory w -- Memory + , SectionIndexMap w -- Section index map + , [MemLoadWarning] -- Warnings from load + ) +memoryForElfSegments'' ignored_segs regIndex addrOff elf = do let hdr = Elf.header elf let cl = Elf.headerClass hdr let w = elfAddrWidth cl @@ -1210,8 +1222,8 @@ memoryForElfSegments' regIndex addrOff elf = do , let shdr = Elf.shdrByIndex elf (idx-1) , let end = Elf.incOffset (Elf.shdrOff shdr) (Elf.shdrFileSize shdr) ] - forM_ phdrs $ \p -> do - when (Elf.phdrSegmentType p == Elf.PT_LOAD) $ do + forM_ (zip [0..] (toList phdrs)) $ \(i, p) -> do + when (Elf.phdrSegmentType p == Elf.PT_LOAD && (not $ elem i ignored_segs)) $ do insertElfSegment regIndex addrOff intervals contents relocMap p -- | Load an elf file into memory by parsing segments. @@ -1226,7 +1238,7 @@ memoryForElfSegments memoryForElfSegments opt elf = do let regIndex = adjustedLoadRegionIndex (Elf.headerType (Elf.header elf)) opt let addrOff = loadRegionBaseOffset opt - memoryForElfSegments' regIndex addrOff elf + memoryForElfSegments'' (ignoreSegments opt) regIndex addrOff elf ------------------------------------------------------------------------ -- Elf section loading diff --git a/base/src/Data/Macaw/Memory/LoadCommon.hs b/base/src/Data/Macaw/Memory/LoadCommon.hs index dfadcc21..41156a7c 100644 --- a/base/src/Data/Macaw/Memory/LoadCommon.hs +++ b/base/src/Data/Macaw/Memory/LoadCommon.hs @@ -27,9 +27,10 @@ data LoadStyle deriving (Eq) -- | This contains options for loading. -newtype LoadOptions = +data LoadOptions = LoadOptions { loadOffset :: Maybe Word64 -- ^ If set, the Elf file should be loaded at a specific offset. + , ignoreSegments :: [Int] } loadRegionIndex :: LoadOptions -> Maybe RegionIndex @@ -45,4 +46,4 @@ loadRegionBaseOffset opts = Just o -> toInteger o defaultLoadOptions :: LoadOptions -defaultLoadOptions = LoadOptions { loadOffset = Nothing } +defaultLoadOptions = LoadOptions { loadOffset = Nothing, ignoreSegments = [] }