Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194-- | Serialise CIE and FDE records with CFA instructions to @.debug_frame@-- section bytes. Inverse of "Data.Dwarf.Read.Frame".module Data.Dwarf.Write.Frame ( writeCfaInstr , writeCfaInstrs , writeCie , writeFde , writeDebugFrame ) where
import Control.Monad (foldM)import Data.Bits ((.&.), (.|.))import Data.ByteString (ByteString)import Data.ByteString.Builder (Builder)import Data.Word (Word8, Word64)
import qualified Data.ByteString as BS
import Data.Dwarf.Encoding (Endianness, Format(..))import Data.Dwarf.Read.Frame (Cie(..), Fde(..), FrameRecord(..), CfaInstr(..))import Data.Dwarf.Write.Writer
-------------------------------------------------------------------------- CFA instruction encoding------------------------------------------------------------------------
-- | Serialise a single CFA instruction.writeCfaInstr :: Endianness -> Word8 -> CfaInstr -> BuilderwriteCfaInstr e addrSize instr = case instr of -- High-2-bit compact forms CfaAdvanceLoc delta -> putU8 (0x40 .|. (delta .&. 0x3F)) CfaOffset reg off -> putU8 (0x80 .|. (reg .&. 0x3F)) <> putULEB128 off CfaRestore reg -> putU8 (0xC0 .|. (reg .&. 0x3F))
-- Full-byte opcodes CfaNop -> putU8 0x00 CfaSetLoc addr -> putU8 0x01 <> putAddress e addrSize addr CfaAdvanceLoc1 delta -> putU8 0x02 <> putU8 delta CfaAdvanceLoc2 delta -> putU8 0x03 <> putU16 e delta CfaAdvanceLoc4 delta -> putU8 0x04 <> putU32 e delta CfaOffsetExtended reg off -> putU8 0x05 <> putULEB128 reg <> putULEB128 off CfaRestoreExtended reg -> putU8 0x06 <> putULEB128 reg CfaUndefined reg -> putU8 0x07 <> putULEB128 reg CfaSameValue reg -> putU8 0x08 <> putULEB128 reg CfaRegister reg1 reg2 -> putU8 0x09 <> putULEB128 reg1 <> putULEB128 reg2 CfaRememberState -> putU8 0x0A CfaRestoreState -> putU8 0x0B CfaDefCfa reg off -> putU8 0x0C <> putULEB128 reg <> putULEB128 off CfaDefCfaRegister reg -> putU8 0x0D <> putULEB128 reg CfaDefCfaOffset off -> putU8 0x0E <> putULEB128 off CfaDefCfaExpression expr -> putU8 0x0F <> putULEB128 (fromIntegral (BS.length expr)) <> putBytes expr CfaExpression reg expr -> putU8 0x10 <> putULEB128 reg <> putULEB128 (fromIntegral (BS.length expr)) <> putBytes expr CfaOffsetExtendedSf reg off -> putU8 0x11 <> putULEB128 reg <> putSLEB128 off CfaDefCfaSf reg off -> putU8 0x12 <> putULEB128 reg <> putSLEB128 off CfaDefCfaOffsetSf off -> putU8 0x13 <> putSLEB128 off CfaValOffset reg off -> putU8 0x14 <> putULEB128 reg <> putULEB128 off CfaValOffsetSf reg off -> putU8 0x15 <> putULEB128 reg <> putSLEB128 off CfaValExpression reg expr -> putU8 0x16 <> putULEB128 reg <> putULEB128 (fromIntegral (BS.length expr)) <> putBytes expr -- GNU extensions CfaGnuWindowSave -> putU8 0x2D CfaGnuArgsSize size -> putU8 0x2E <> putULEB128 size
-- | Serialise a list of CFA instructions.writeCfaInstrs :: Endianness -> Word8 -> [CfaInstr] -> BuilderwriteCfaInstrs e addrSize = foldMap (writeCfaInstr e addrSize)
-------------------------------------------------------------------------- CIE serialisation------------------------------------------------------------------------
-- | Serialise a CIE record to bytes (including initial_length).writeCie :: Endianness -> Cie -> ByteStringwriteCie e cie = let fmt = cieFormat cie ver = cieVersion cie as = cieAddressSize cie aug = cieAugmentation cie zAug = hasZAug aug
-- CIE content (after initial_length and CIE_id sentinel) content = putU8 ver -- version <> putNullTerminated aug -- augmentation string <> (if ver >= 4 then putU8 as <> putU8 (cieSegmentSelectorSize cie) else mempty) <> putULEB128 (cieCodeAlignmentFactor cie) <> putSLEB128 (cieDataAlignmentFactor cie) <> (if ver == 1 then putU8 (fromIntegral (cieReturnAddressRegister cie)) else putULEB128 (cieReturnAddressRegister cie)) <> (if zAug then let augData = cieAugmentationData cie in putULEB128 (fromIntegral (BS.length augData)) <> putBytes augData else mempty) <> writeCfaInstrs e as (cieInitialInstructions cie)
contentBS = toStrict content
-- CIE_id sentinel sentinel = case fmt of Dwarf32 -> putU32 e 0xFFFFFFFF Dwarf64 -> putU64 e 0xFFFFFFFFFFFFFFFF
sentinelBS = toStrict sentinel totalContent = BS.length sentinelBS + BS.length contentBS
in toStrict (putInitialLength e fmt (fromIntegral totalContent) <> putBytes sentinelBS <> putBytes contentBS)
-------------------------------------------------------------------------- FDE serialisation------------------------------------------------------------------------
-- | Serialise an FDE record to bytes (including initial_length).-- The @Word64@ parameter is the absolute offset of the associated CIE-- within @.debug_frame@.writeFde :: Endianness -> Cie -> Word64 -> Fde -> ByteStringwriteFde e cie cieAbsOff fde = let fmt = fdeFormat fde as = cieAddressSize cie ss = cieSegmentSelectorSize cie zAug = hasZAug (cieAugmentation cie)
-- FDE content (after initial_length and CIE_pointer) content = (if ss > 0 then putZeros (fromIntegral ss) else mempty) <> putAddress e as (fdeInitialLocation fde) <> putAddress e as (fdeAddressRange fde) <> (if zAug then let augData = fdeAugmentationData fde in putULEB128 (fromIntegral (BS.length augData)) <> putBytes augData else mempty) <> writeCfaInstrs e as (fdeInstructions fde)
contentBS = toStrict content
-- CIE pointer (absolute offset of associated CIE) ciePtr = putOffset e fmt cieAbsOff ciePtrBS = toStrict ciePtr
totalContent = BS.length ciePtrBS + BS.length contentBS
in toStrict (putInitialLength e fmt (fromIntegral totalContent) <> putBytes ciePtrBS <> putBytes contentBS)
-------------------------------------------------------------------------- Full section------------------------------------------------------------------------
-- | Serialise a list of frame records into a @.debug_frame@ section.---- CIE offsets are tracked so FDEs can reference them. Each CIE is-- assigned its absolute offset as it is written.---- Returns 'Nothing' if an FDE references a CIE that has not been seen.writeDebugFrame :: Endianness -> [FrameRecord] -> Maybe ByteStringwriteDebugFrame e records = do (chunks, _, _) <- foldM go ([], [], 0 :: Word64) records Just (BS.concat chunks) where go (!acc, !cies, !off) (FrameCie cie) = let bs = writeCie e cie len = fromIntegral (BS.length bs) :: Word64 in Just (acc ++ [bs], (off, cie) : cies, off + len) go (!acc, !cies, !off) (FrameFde fde) = do let cieOff = fdeCieOffset fde cie <- case lookup cieOff (reverse cies) of Just c -> Just c Nothing -> case cies of ((_, c):_) -> Just c [] -> Nothing let bs = writeFde e cie cieOff fde len = fromIntegral (BS.length bs) :: Word64 Just (acc ++ [bs], cies, off + len)
-------------------------------------------------------------------------- Internal helpers------------------------------------------------------------------------
-- | Check if an augmentation string starts with @'z'@.hasZAug :: ByteString -> BoolhasZAug bs = not (BS.null bs) && BS.index bs 0 == 0x7A -- 'z'