-- | Serialise DWARF units with headers. module Data.Dwarf.Write.Unit ( writeUnit , writeDebugInfo , writeDebugInfoPooled , writeDebugInfoDwoPooled , writeDebugTypes , writeDebugTypesPooled , unitTypeCode -- * Skeleton construction , SkeletonConfig(..) , buildSkeletonUnit ) where import Control.Monad (foldM) import Data.ByteString (ByteString) import Data.ByteString.Builder (Builder) import Data.Word (Word8, Word64) import qualified Data.ByteString as BS import qualified Data.IntMap.Strict as IntMap import qualified Data.Vector as Vector import Data.Dwarf.Constants ( DwUt(..), DwTag(..) , pattern DW_TAG_compile_unit , pattern DW_AT_dwo_name, pattern DW_AT_comp_dir , pattern DW_AT_addr_base, pattern DW_AT_str_offsets_base , pattern DW_AT_rnglists_base, pattern DW_AT_stmt_list ) import Data.Dwarf.Encoding (Endianness, Encoding(..), Format(..)) import Data.Dwarf.Read.Abbrev (AbbrevTable(..)) import Data.Dwarf.Read.Attr (AttrVal(..)) import Data.Dwarf.Read.Die (Attr(..), Die(..), DieTree(..)) import Data.Dwarf.Read.Unit (Unit(..), UnitType(..)) import Data.Dwarf.Types (DebugAbbrevOffset(..), TypeSignature(..), DwoId(..), UnitOffset(..)) import Data.Dwarf.Write.Abbrev (writeAbbrevTable) import Data.Dwarf.Write.Die (writeDieForest, writeDieForestPooled, writeDieForestDwoPooled) import Data.Dwarf.Write.Str ( emptyStringTable, buildDebugStr , emptyIndexedStringTable, buildDebugStrDwo, indexedStringOffsets ) import Data.Dwarf.Write.StrOffsets (writeStrOffsetsTable) import Data.Dwarf.Write.Writer ------------------------------------------------------------------------ -- Unit serialisation ------------------------------------------------------------------------ -- | Wrap a pre-serialised DIE body in a unit header. -- Returns the complete unit bytes (.debug_info contribution). writeUnit :: Endianness -> Encoding -> UnitType -> DebugAbbrevOffset -> ByteString -- ^ Pre-serialised DIE body -> ByteString writeUnit e enc ut abbrevOff dieBody = toStrict $ writeUnitBuilder e enc ut abbrevOff (putBytes dieBody) -- | Build a unit with header around a DIE body builder. writeUnitBuilder :: Endianness -> Encoding -> UnitType -> DebugAbbrevOffset -> Builder -- ^ DIE body builder -> Builder writeUnitBuilder e enc ut (DebugAbbrevOffset abbrevOff) dieBody = let ver = encodingVersion enc fmt = encodingFormat enc as = encodingAddressSize enc -- Compute the header fields (after initial length) headerFields = writeHeaderFields e ver fmt as ut abbrevOff headerBS = toStrict headerFields bodyBS = toStrict dieBody contentLen = fromIntegral (BS.length headerBS + BS.length bodyBS) :: Word64 in putInitialLength e fmt contentLen <> putBytes headerBS <> putBytes bodyBS -- | Write the header fields between the initial length and the DIE body. writeHeaderFields :: Endianness -> Int -> Format -> Word8 -> UnitType -> Word64 -> Builder writeHeaderFields e ver fmt addrSize ut abbrevOff | ver >= 5 = -- DWARF 5: version -> unit_type -> address_size -> abbrev_offset -> type-specific putU16 e (fromIntegral ver) <> putU8 (unitTypeCode ut) <> putU8 addrSize <> putOffset e fmt abbrevOff <> writeUnitTypeFields e fmt ut | otherwise = -- DWARF 2/3/4: version -> abbrev_offset -> address_size (no unit_type) putU16 e (fromIntegral ver) <> putOffset e fmt abbrevOff <> putU8 addrSize <> writeUnitTypeFields e fmt ut -- | Write the unit-type-specific trailing header fields. writeUnitTypeFields :: Endianness -> Format -> UnitType -> Builder writeUnitTypeFields _ _ UnitCompile = mempty writeUnitTypeFields _ _ UnitPartial = mempty writeUnitTypeFields e fmt (UnitType (TypeSignature sig) (UnitOffset off)) = putU64 e sig <> putOffset e fmt off writeUnitTypeFields e _ (UnitSkeleton (DwoId dwoId)) = putU64 e dwoId writeUnitTypeFields e _ (UnitSplitCompile (DwoId dwoId)) = putU64 e dwoId writeUnitTypeFields e fmt (UnitSplitType (TypeSignature sig) (UnitOffset off)) = putU64 e sig <> putOffset e fmt off -- | Map UnitType to its DW_UT code byte. unitTypeCode :: UnitType -> Word8 unitTypeCode UnitCompile = let DwUt w = DW_UT_compile in w unitTypeCode UnitPartial = let DwUt w = DW_UT_partial in w unitTypeCode (UnitType _ _) = let DwUt w = DW_UT_type in w unitTypeCode (UnitSkeleton _) = let DwUt w = DW_UT_skeleton in w unitTypeCode (UnitSplitCompile _) = let DwUt w = DW_UT_split_compile in w unitTypeCode (UnitSplitType _ _) = let DwUt w = DW_UT_split_type in w ------------------------------------------------------------------------ -- High-level ------------------------------------------------------------------------ -- | Serialise a list of units into paired (.debug_info, .debug_abbrev) sections. -- -- Each unit gets its own abbreviation table. All abbreviation tables are -- concatenated into a single .debug_abbrev section, and each unit's header -- points to its table's offset within that section. -- -- Returns 'Nothing' if a DIE shape lookup fails. writeDebugInfo :: Endianness -> Encoding -> [Unit] -> Maybe (ByteString, ByteString) writeDebugInfo e _enc units = do (infoChunks, abbrevBuilder, _) <- foldM go ([], mempty, 0) units let debugInfo = BS.concat infoChunks debugAbbrev = toStrict abbrevBuilder Just (debugInfo, debugAbbrev) where go (!infos, !abbrev, !abbrevOff) u = do let forest = unitDieForest u uEnc = unitEnc u (table, dieBody) <- writeDieForest e uEnc forest let abbrevBS = writeAbbrevTable table abbrevLen = fromIntegral (BS.length abbrevBS) :: Word64 bodyBS = toStrict dieBody unitBS = writeUnit e uEnc (unitType u) (DebugAbbrevOffset abbrevOff) bodyBS Just (infos ++ [unitBS], abbrev <> putBytes abbrevBS, abbrevOff + abbrevLen) -- | Like 'writeDebugInfo' but pools all 'ValString' attributes into a -- shared @.debug_str@ section, replacing them with 'ValStrOffset' ('DW_FORM_strp'). -- Returns @(debug_info, debug_abbrev, debug_str)@, or 'Nothing' if a DIE -- shape lookup fails. writeDebugInfoPooled :: Endianness -> Encoding -> [Unit] -> Maybe (ByteString, ByteString, ByteString) writeDebugInfoPooled e _enc units = do (infoChunks, abbrevBuilder, _, strTable) <- foldM go ([], mempty, 0, emptyStringTable) units let debugInfo = BS.concat infoChunks debugAbbrev = toStrict abbrevBuilder debugStr = buildDebugStr strTable Just (debugInfo, debugAbbrev, debugStr) where go (!infos, !abbrev, !abbrevOff, !st) u = do let forest = unitDieForest u uEnc = unitEnc u (table, dieBody, st') <- writeDieForestPooled e uEnc forest st let abbrevBS = writeAbbrevTable table abbrevLen = fromIntegral (BS.length abbrevBS) :: Word64 bodyBS = toStrict dieBody unitBS = writeUnit e uEnc (unitType u) (DebugAbbrevOffset abbrevOff) bodyBS Just (infos ++ [unitBS], abbrev <> putBytes abbrevBS, abbrevOff + abbrevLen, st') -- | Like 'writeDebugInfoPooled' but for split DWARF (.dwo) output. -- Pools all 'ValString' attributes into an 'IndexedStringTable', -- replacing them with 'ValStrIndex' ('DW_FORM_strx'). -- -- Returns @(debug_info_dwo, debug_abbrev_dwo, debug_str_dwo, debug_str_offsets_dwo)@, -- or 'Nothing' if a DIE shape lookup fails. writeDebugInfoDwoPooled :: Endianness -> Encoding -> [Unit] -> Maybe (ByteString, ByteString, ByteString, ByteString) writeDebugInfoDwoPooled e _enc units = do (infoChunks, abbrevBuilder, _, ist) <- foldM go ([], mempty, 0, emptyIndexedStringTable) units let debugInfoDwo = BS.concat infoChunks debugAbbrevDwo = toStrict abbrevBuilder debugStrDwo = buildDebugStrDwo ist fmt = encodingFormat _enc debugStrOffsetsDwo = writeStrOffsetsTable e fmt (indexedStringOffsets ist) Just (debugInfoDwo, debugAbbrevDwo, debugStrDwo, debugStrOffsetsDwo) where go (!infos, !abbrev, !abbrevOff, !ist) u = do let forest = unitDieForest u uEnc = unitEnc u (table, dieBody, ist') <- writeDieForestDwoPooled e uEnc forest ist let abbrevBS = writeAbbrevTable table abbrevLen = fromIntegral (BS.length abbrevBS) :: Word64 bodyBS = toStrict dieBody unitBS = writeUnit e uEnc (unitType u) (DebugAbbrevOffset abbrevOff) bodyBS Just (infos ++ [unitBS], abbrev <> putBytes abbrevBS, abbrevOff + abbrevLen, ist') -- | Serialise a list of DWARF 4 type units into paired -- (@.debug_types@, @.debug_abbrev@) sections. -- -- The format is identical to @.debug_info@ — only the unit headers -- carry 'UnitType' with a type signature and type offset. writeDebugTypes :: Endianness -> Encoding -> [Unit] -> Maybe (ByteString, ByteString) writeDebugTypes = writeDebugInfo -- | Like 'writeDebugTypes' but pools all 'ValString' attributes into a -- shared @.debug_str@ section, replacing them with 'ValStrOffset' -- ('DW_FORM_strp'). -- Returns @(debug_types, debug_abbrev, debug_str)@, or 'Nothing' if a -- DIE shape lookup fails. writeDebugTypesPooled :: Endianness -> Encoding -> [Unit] -> Maybe (ByteString, ByteString, ByteString) writeDebugTypesPooled = writeDebugInfoPooled ------------------------------------------------------------------------ -- Skeleton construction ------------------------------------------------------------------------ -- | Configuration for building a skeleton compilation unit. -- The skeleton lives in the main binary and points to the @.dwo@ file. data SkeletonConfig = SkeletonConfig { skelDwoName :: !ByteString -- ^ @DW_AT_dwo_name@ value (path to .dwo file). , skelCompDir :: !(Maybe ByteString) -- ^ @DW_AT_comp_dir@ (compilation directory). , skelAddrBase :: !(Maybe Word64) -- ^ @DW_AT_addr_base@ (offset into @.debug_addr@). , skelStrOffsetsBase :: !(Maybe Word64) -- ^ @DW_AT_str_offsets_base@. , skelRngListsBase :: !(Maybe Word64) -- ^ @DW_AT_rnglists_base@. , skelStmtList :: !(Maybe Word64) -- ^ @DW_AT_stmt_list@ (offset into @.debug_line@). } deriving stock (Show) -- | Build a skeleton compilation unit from a 'SkeletonConfig' and DWO ID. -- -- The resulting unit has @DW_UT_skeleton@ type and contains a single -- @DW_TAG_compile_unit@ DIE with the configured attributes. buildSkeletonUnit :: Encoding -> DwoId -> SkeletonConfig -> Unit buildSkeletonUnit enc dwoId cfg = let attrs = [Attr DW_AT_dwo_name (ValString (skelDwoName cfg))] ++ maybe [] (\d -> [Attr DW_AT_comp_dir (ValString d)]) (skelCompDir cfg) ++ maybe [] (\v -> [Attr DW_AT_addr_base (ValSecOffset v)]) (skelAddrBase cfg) ++ maybe [] (\v -> [Attr DW_AT_str_offsets_base (ValSecOffset v)]) (skelStrOffsetsBase cfg) ++ maybe [] (\v -> [Attr DW_AT_rnglists_base (ValSecOffset v)]) (skelRngListsBase cfg) ++ maybe [] (\v -> [Attr DW_AT_stmt_list (ValSecOffset v)]) (skelStmtList cfg) die = Die DW_TAG_compile_unit False (Vector.fromList attrs) tree = DieTree die Vector.empty in Unit { unitEnc = enc , unitType = UnitSkeleton dwoId , unitAbbrevs = AbbrevTable IntMap.empty , unitDieForest = Vector.singleton tree }