-- | Build @.debug_str@ and @.debug_line_str@ sections with string deduplication. -- Also provides 'IndexedStringTable' for @.debug_str.dwo@ (split DWARF) -- where strings are referenced by index ('DW_FORM_strx') rather than -- offset ('DW_FORM_strp'). module Data.Dwarf.Write.Str ( StringTable , emptyStringTable , addString , buildDebugStr , buildDebugLineStr -- * Indexed string tables (split DWARF) , IndexedStringTable , emptyIndexedStringTable , addIndexedString , buildDebugStrDwo , indexedStringOffsets ) where import Data.ByteString (ByteString) import Data.ByteString.Builder (Builder) import Data.Map.Strict (Map) import Data.Word (Word64) import qualified Data.ByteString as BS import qualified Data.ByteString.Builder as B import qualified Data.Map.Strict as Map import Data.Dwarf.Write.Writer (toStrict) -- | A string table that tracks offsets and deduplicates strings. -- Used to build the @.debug_str@ section. data StringTable = StringTable { stMap :: !(Map ByteString Word64) -- ^ Mapping from string content to its offset. , stBuilder :: !Builder -- ^ Accumulated NUL-terminated string bytes. , stNextOff :: !Word64 -- ^ Next available write offset. } -- | An empty string table. emptyStringTable :: StringTable emptyStringTable = StringTable Map.empty mempty 0 -- | Add a string to the table. Returns the offset of the string -- (reusing an existing offset if the string was already added) and -- the updated table. addString :: ByteString -> StringTable -> (Word64, StringTable) addString s st = case Map.lookup s (stMap st) of Just off -> (off, st) Nothing -> let off = stNextOff st len = fromIntegral (BS.length s) + 1 -- string + NUL st' = st { stMap = Map.insert s off (stMap st) , stBuilder = stBuilder st <> B.byteString s <> B.word8 0 , stNextOff = off + len } in (off, st') -- | Finalise the string table to a @.debug_str@ section ByteString. buildDebugStr :: StringTable -> ByteString buildDebugStr = toStrict . stBuilder -- | Finalise the string table to a @.debug_line_str@ section ByteString. -- The format is identical to @.debug_str@ (flat NUL-terminated string pool). buildDebugLineStr :: StringTable -> ByteString buildDebugLineStr = toStrict . stBuilder ------------------------------------------------------------------------ -- Indexed string table (split DWARF) ------------------------------------------------------------------------ -- | A string table that assigns 0-based indices for @DW_FORM_strx@. -- Used to build @.debug_str.dwo@ and @.debug_str_offsets.dwo@ sections. data IndexedStringTable = IndexedStringTable { istMap :: !(Map ByteString Word64) -- ^ String → index , istOffsets :: ![Word64] -- ^ Reversed list of offsets into the string blob , istBuilder :: !Builder -- ^ Accumulated NUL-terminated string bytes , istNextOff :: !Word64 -- ^ Next byte offset in the string blob , istNextIdx :: !Word64 -- ^ Next index to assign } -- | An empty indexed string table. emptyIndexedStringTable :: IndexedStringTable emptyIndexedStringTable = IndexedStringTable Map.empty [] mempty 0 0 -- | Add a string, returning its 0-based index. Deduplicates. addIndexedString :: ByteString -> IndexedStringTable -> (Word64, IndexedStringTable) addIndexedString s ist = case Map.lookup s (istMap ist) of Just idx -> (idx, ist) Nothing -> let idx = istNextIdx ist off = istNextOff ist len = fromIntegral (BS.length s) + 1 -- string + NUL ist' = ist { istMap = Map.insert s idx (istMap ist) , istOffsets = off : istOffsets ist , istBuilder = istBuilder ist <> B.byteString s <> B.word8 0 , istNextOff = off + len , istNextIdx = idx + 1 } in (idx, ist') -- | Finalise the indexed string table to @.debug_str.dwo@ bytes. buildDebugStrDwo :: IndexedStringTable -> ByteString buildDebugStrDwo = toStrict . istBuilder -- | Return the list of byte offsets for 'writeStrOffsetsTable'. -- The list is in index order (index 0 first). indexedStringOffsets :: IndexedStringTable -> [Word64] indexedStringOffsets = reverse . istOffsets