From ffa802f5834e0af5663bd902c7ff07789dd85069 Mon Sep 17 00:00:00 2001 From: Nikita Smith Date: Wed, 1 May 2024 16:54:15 -0700 Subject: [PATCH] codeview: inline binary annot format and decode helpers --- src/codeview/codeview.c | 68 +++++++++++++++++++++++++++++++++++++++++ src/codeview/codeview.h | 58 +++++++++++++++++++++++++++-------- 2 files changed, 113 insertions(+), 13 deletions(-) diff --git a/src/codeview/codeview.c b/src/codeview/codeview.c index ad1b528e..06b2d2c8 100644 --- a/src/codeview/codeview.c +++ b/src/codeview/codeview.c @@ -165,6 +165,74 @@ cv_f64_from_numeric(CV_NumericParsed *num){ return(result); } +//////////////////////////////// +//~ Inline Binary Annotation Helpers + +internal U64 +cv_decode_inline_annot_u32(String8 data, U64 offset, U32 *out_value) +{ + U32 value; + + U64 cursor = offset; + + U8 header = 0; + cursor += str8_deserial_read_struct(data, cursor, &header); + + // 1 byte + if((header & 0x80) == 0) + { + value = header; + } + // 2 bytes + else if((header & 0xC0) == 0x80) + { + Assert(cursor + sizeof(U8) * 2 <= data.size); + U8 second_byte; + cursor += str8_deserial_read_struct(data, cursor, &second_byte); + value = ((header & 0x3F) << 8) | second_byte; + } + // 4 bytes + else if((header & 0xE0) == 0xC0) + { + Assert(cursor + sizeof(U8) * 3 <= data.size); + U8 second_byte, third_byte, fourth_byte; + cursor += str8_deserial_read_struct(data, cursor, &second_byte); + cursor += str8_deserial_read_struct(data, cursor, &third_byte); + cursor += str8_deserial_read_struct(data, cursor, &fourth_byte); + value = (((U32)header & 0x1F) << 24) | ((U32)second_byte << 16) | ((U32)third_byte << 8) | (U32)fourth_byte; + } + // bad encode + else if((header & 0xE0) == 0xE0) + { + value = max_U32; + } + + *out_value = value; + + U64 read_size = cursor - offset; + return read_size; +} + +internal U64 +cv_decode_inline_annot_s32(String8 data, U64 offset, S32 *out_value) +{ + U32 value; + + U64 read_size = cv_decode_inline_annot_u32(data, offset, &value); + + if(value & 1) + { + value = -(value >> 1); + } + else + { + value = value >> 1; + } + + *out_value = (S32)value; + + return read_size; +} //////////////////////////////// //~ CodeView Sym Parser Functions diff --git a/src/codeview/codeview.h b/src/codeview/codeview.h index a037bee2..ff9f348e 100644 --- a/src/codeview/codeview.h +++ b/src/codeview/codeview.h @@ -1761,13 +1761,51 @@ struct CV_SymBuildInfo //- (SymKind: INLINESITE) +typedef U32 CV_InlineBinaryAnnotaiton; +typedef enum CV_InlineBinaryAnnotationenum +{ + CV_InlineBinaryAnnotation_Null, + CV_InlineBinaryAnnotation_CodeOffset, + CV_InlineBinaryAnnotation_ChangeCodeOffsetBase, + CV_InlineBinaryAnnotation_ChangeCodeOffset, + CV_InlineBinaryAnnotation_ChangeCodeLength, + CV_InlineBinaryAnnotation_ChnageFile, + CV_InlineBinaryAnnotation_ChangeLineOffset, + CV_InlineBinaryAnnotation_ChnageLineEndDelta, + CV_InlineBinaryAnnotation_ChangeRangeKind, + CV_InlineBinaryAnnotation_ChangeColumnStart, + CV_InlineBinaryAnnotation_ChangeColumnEndDelta, + CV_InlineBinaryAnnotation_ChangeCodeOffsetAndLineOffset, + CV_InlineBinaryAnnotation_ChangeCodeLengthAndCodeOffset, + CV_InlineBinaryAnnotaiton_ChangeColumnEnd +}; + +typedef U32 CV_InlineRangeKind; +typedef enum CV_InlnineRangeKindEnum +{ + CV_InlineRangeKind_Expr, + CV_InlineRangeKind_Stmt +}; + typedef struct CV_SymInlineSite CV_SymInlineSite; struct CV_SymInlineSite { U32 parent; U32 end; CV_ItemId inlinee; - // CV_BinaryAnnotation annotations (rest of data) + // U8 annotations[] (rest of data) +}; + +//- (SymKind: INLINESITE2) + +typedef struct CV_SymInlineSite2 CV_SymInlineSite2; +struct CV_SymInlineSite2 +{ + U32 parent_off; + U32 end_off; + CV_ItemId inlinee; + U32 invocations; + // U8 annotations[] (rest of data) }; //- (SymKind: INLINESITE_END) (empty) @@ -1835,18 +1873,6 @@ struct CV_SymPogoInfo U32 post_inline_static_inst_count; }; -//- (SymKind: INLINESITE2) - -typedef struct CV_SymInlineSite2 CV_SymInlineSite2; -struct CV_SymInlineSite2 -{ - U32 parent_off; - U32 end_off; - CV_ItemId inlinee; - U32 invocations; - // CV_BinaryAnnotation annotations (rest of data) -}; - //- (SymKind: HEAPALLOCSITE) typedef struct CV_SymHeapAllocSite CV_SymHeapAllocSite; @@ -2905,6 +2931,12 @@ internal U64 cv_u64_from_numeric(CV_NumericParsed *num); internal S64 cv_s64_from_numeric(CV_NumericParsed *num); internal F64 cv_f64_from_numeric(CV_NumericParsed *num); +//////////////////////////////// +//~ Inline Binary Annotation Helpers + +internal U64 cv_decode_inline_annot_u32(String8 data, U64 offset, U32 *out_value); +internal U64 cv_decode_inline_annot_s32(String8 data, U64 offset, S32 *out_value); + //////////////////////////////// //~ CodeView Sym/Leaf Parser Functions