Skip to content

Commit

Permalink
hack to prevent loading certain segments
Browse files Browse the repository at this point in the history
  • Loading branch information
danmatichuk committed Nov 1, 2023
1 parent 694a427 commit 7b40596
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
22 changes: 17 additions & 5 deletions base/src/Data/Macaw/Memory/ElfLoader.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1195,7 +1195,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
Expand All @@ -1205,7 +1204,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
Expand All @@ -1223,8 +1235,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.
Expand All @@ -1239,7 +1251,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
Expand Down
5 changes: 3 additions & 2 deletions base/src/Data/Macaw/Memory/LoadCommon.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -45,4 +46,4 @@ loadRegionBaseOffset opts =
Just o -> toInteger o

defaultLoadOptions :: LoadOptions
defaultLoadOptions = LoadOptions { loadOffset = Nothing }
defaultLoadOptions = LoadOptions { loadOffset = Nothing, ignoreSegments = [] }

0 comments on commit 7b40596

Please sign in to comment.