Something went wrong. Try again.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221-- | Serialise a DWARF 5 @.debug_names@ contribution.---- Format: initial_length, header (version, padding, counts, augmentation),-- CU offsets, local TU offsets, foreign TU signatures,-- hash buckets, hash values, string offsets, entry offsets,-- abbreviation table, entry pool.---- Inverse of "Data.Dwarf.Read.Names".module Data.Dwarf.Write.Names ( writeNameAbbrevs , writeEntryChain , writeNameIndex , writeDebugNames ) where
import Data.ByteString (ByteString)import Data.ByteString.Builder (Builder)import Data.Int (Int64)import Data.IntMap.Strict (IntMap)import Data.List (find)import Data.Vector (Vector)import Data.Word (Word32, Word64)
import qualified Data.ByteString as BSimport qualified Data.IntMap.Strict as IntMapimport qualified Data.Vector as Vector
import Data.Dwarf.Constants (DwForm(..), DwIdx(..), DwTag(..))import Data.Dwarf.Encoding (Endianness, Format(..))import Data.Dwarf.Read.Names ( NamesHeader(..) , NameAbbrevAttr(..) , NameAbbrev(..) , NameAttr(..) , NameEntry(..) , NameIndex(..) )import Data.Dwarf.Write.Writer
-------------------------------------------------------------------------- Abbreviation table------------------------------------------------------------------------
-- | Serialise a single abbreviation attribute spec.writeNameAbbrevAttr :: NameAbbrevAttr -> BuilderwriteNameAbbrevAttr attr = let DwIdx idx = naaIdx attr DwForm form = naaForm attr in putULEB128 (fromIntegral idx) <> putULEB128 (fromIntegral form) <> case naaImplicitConst attr of Just v -> putSLEB128 v Nothing -> mempty
-- | Serialise a @.debug_names@ abbreviation table.-- Each entry: ULEB128 code, ULEB128 tag, attr specs, (0,0) terminator.-- Table ends with a 0 code byte.writeNameAbbrevs :: IntMap NameAbbrev -> BuilderwriteNameAbbrevs tbl = IntMap.foldlWithKey' (\acc code abbrev -> acc <> writeOneAbbrev code abbrev) mempty tbl <> putULEB128 0 -- table terminator
writeOneAbbrev :: Int -> NameAbbrev -> BuilderwriteOneAbbrev code abbrev = let DwTag tag = nameAbbrevTag abbrev in putULEB128 (fromIntegral code) <> putULEB128 (fromIntegral tag) <> foldMap writeNameAbbrevAttr (nameAbbrevAttrs abbrev) <> putULEB128 0 <> putULEB128 0 -- attr list terminator
-------------------------------------------------------------------------- Entry pool------------------------------------------------------------------------
-- | Serialise a single entry's attribute value according to its form.writeEntryAttrVal :: Endianness -> NameAbbrevAttr -> Word64 -> BuilderwriteEntryAttrVal e spec val | form == DW_FORM_data1 = putU8 (fromIntegral val) | form == DW_FORM_data2 = putU16 e (fromIntegral val) | form == DW_FORM_data4 = putU32 e (fromIntegral val) | form == DW_FORM_data8 = putU64 e val | form == DW_FORM_udata = putULEB128 val | form == DW_FORM_sdata = putSLEB128 (fromIntegral val :: Int64) | form == DW_FORM_ref4 = putU32 e (fromIntegral val) | form == DW_FORM_flag_present = mempty | form == DW_FORM_implicit_const = mempty | otherwise = mempty where form = naaForm spec
-- | Serialise a chain of entries for one name in the entry pool.-- Each entry: ULEB128 abbrev_code + attribute values; chain ends with 0.-- Returns 'Nothing' if an entry has no matching abbreviation or is missing-- a required attribute.writeEntryChain :: Endianness -> IntMap NameAbbrev -> [NameEntry] -> Maybe BuilderwriteEntryChain e abbrevs entries = do builders <- mapM (writeOneEntry e abbrevs) entries Just (mconcat builders <> putULEB128 0)
writeOneEntry :: Endianness -> IntMap NameAbbrev -> NameEntry -> Maybe BuilderwriteOneEntry e abbrevs entry = do (code, abbrev) <- findAbbrev (nameEntryTag entry) (nameEntryAttrs entry) vals <- mapM (\spec -> do val <- lookupAttrVal (naaIdx spec) (nameEntryAttrs entry) Just (writeEntryAttrVal e spec val) ) (nameAbbrevAttrs abbrev) Just (putULEB128 (fromIntegral code) <> mconcat vals) where findAbbrev tag attrs = case IntMap.toList (IntMap.filter match abbrevs) of ((c, a):_) -> Just (c, a) [] -> Nothing where match ab = nameAbbrevTag ab == tag && length (nameAbbrevAttrs ab) == length attrs && all (\(spec, attr) -> naaIdx spec == nameAttrIdx attr) (zip (nameAbbrevAttrs ab) attrs)
lookupAttrVal idx attrVals = nameAttrValue <$> find (\a -> nameAttrIdx a == idx) attrVals
-------------------------------------------------------------------------- Full name index------------------------------------------------------------------------
-- | Serialise a complete @.debug_names@ contribution from a 'NameIndex'.-- Returns 'Nothing' if an entry has no matching abbreviation or is missing-- a required attribute.writeNameIndex :: Endianness -> NameIndex -> Maybe ByteStringwriteNameIndex e ni = do let hdr = niHeader ni fmt = namesFormat hdr augStr = namesAugmentation hdr
-- Serialise the abbreviation table to compute its size abbrBuilder = writeNameAbbrevs (niAbbrevs ni) abbrBs = toStrict abbrBuilder abbrSz = fromIntegral (BS.length abbrBs) :: Word32
-- Serialise entry chains and compute offsets chainBytesList <- mapM (\chain -> fmap toStrict (writeEntryChain e (niAbbrevs ni) chain)) (Vector.toList (niEntries ni)) let chainBytesVec = Vector.fromList chainBytesList chainSizes = Vector.map (fromIntegral . BS.length) chainBytesVec :: Vector Word64 entryOffsets = Vector.prescanl' (+) 0 chainSizes
-- Header fields (after initial_length) headerBuilder = putU16 e 5 -- version <> putU16 e 0 -- padding <> putU32 e (namesCompUnitCount hdr) <> putU32 e (namesLocalTypeUnitCount hdr) <> putU32 e (namesForeignTypeUnitCount hdr) <> putU32 e (namesBucketCount hdr) <> putU32 e (namesNameCount hdr) <> putU32 e abbrSz <> putU32 e (fromIntegral (BS.length augStr)) <> putBytes augStr
-- CU offsets cuBuilder = vFoldOffset e fmt (niCompUnits ni)
-- Local TU offsets ltuBuilder = vFoldOffset e fmt (niLocalTUs ni)
-- Foreign TU signatures (always 8 bytes each) ftuBuilder = Vector.foldl' (\acc sig -> acc <> putU64 e sig) mempty (niForeignTUs ni)
-- Hash buckets bucketBuilder = Vector.foldl' (\acc b -> acc <> putU32 e b) mempty (niBuckets ni)
-- Hash values hashBuilder = Vector.foldl' (\acc h -> acc <> putU32 e h) mempty (niHashes ni)
-- String offsets strOffBuilder = vFoldOffset e fmt (niStringOffsets ni)
-- Entry offsets entryOffBuilder = vFoldOffset e fmt entryOffsets
-- Entry pool poolBuilder = Vector.foldl' (\acc bs -> acc <> putBytes bs) mempty chainBytesVec
-- All content after initial_length content = headerBuilder <> cuBuilder <> ltuBuilder <> ftuBuilder <> bucketBuilder <> hashBuilder <> strOffBuilder <> entryOffBuilder <> putBytes abbrBs <> poolBuilder
contentBs = toStrict content contentLen = fromIntegral (BS.length contentBs) :: Word64
full = putInitialLength e fmt contentLen <> putBytes contentBs
Just (toStrict full)
-- | Serialise multiple @.debug_names@ contributions concatenated.-- Returns 'Nothing' if any contribution fails.writeDebugNames :: Endianness -> [NameIndex] -> Maybe ByteStringwriteDebugNames e idxs = do bss <- mapM (writeNameIndex e) idxs Just (toStrict (foldMap putBytes bss))
-------------------------------------------------------------------------- Internals------------------------------------------------------------------------
-- | Fold a vector of offsets into a Builder.vFoldOffset :: Endianness -> Format -> Vector Word64 -> BuildervFoldOffset e fmt = Vector.foldl' (\acc off -> acc <> putOffset e fmt off) mempty