Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248-- | 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 BSimport qualified Data.IntMap.Strict as IntMapimport 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 -> ByteStringwriteUnit 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 -> BuilderwriteUnitBuilder 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 -> BuilderwriteHeaderFields 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 -> BuilderwriteUnitTypeFields _ _ UnitCompile = memptywriteUnitTypeFields _ _ UnitPartial = memptywriteUnitTypeFields e fmt (UnitType (TypeSignature sig) (UnitOffset off)) = putU64 e sig <> putOffset e fmt offwriteUnitTypeFields e _ (UnitSkeleton (DwoId dwoId)) = putU64 e dwoIdwriteUnitTypeFields e _ (UnitSplitCompile (DwoId dwoId)) = putU64 e dwoIdwriteUnitTypeFields e fmt (UnitSplitType (TypeSignature sig) (UnitOffset off)) = putU64 e sig <> putOffset e fmt off
-- | Map UnitType to its DW_UT code byte.unitTypeCode :: UnitType -> Word8unitTypeCode UnitCompile = let DwUt w = DW_UT_compile in wunitTypeCode UnitPartial = let DwUt w = DW_UT_partial in wunitTypeCode (UnitType _ _) = let DwUt w = DW_UT_type in wunitTypeCode (UnitSkeleton _) = let DwUt w = DW_UT_skeleton in wunitTypeCode (UnitSplitCompile _) = let DwUt w = DW_UT_split_compile in wunitTypeCode (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 -> UnitbuildSkeletonUnit 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 }