@@ -321,11 +321,20 @@ static Error decodeFixup(const FlatV1ObjectReader &Reader, StringRef &Data,
321321}
322322
323323Expected<BlockRef> BlockRef::create (CompileUnitBuilder &CUB,
324- const jitlink::Block &Block) {
324+ const jitlink::Block &Block,
325+ cas::CASID *AbbrevID) {
325326 Expected<Builder> B = Builder::startNode (CUB.Schema , KindString);
326327 if (!B)
327328 return B.takeError ();
328329
330+ bool IsDebugInfoBlock = false ;
331+ // If we are creating a cas block out of a debug_info jitlink::Block, add the
332+ // debug_abbrev cas block CAS ID as a refrence
333+ if (Block.getSection ().getName () == " __DWARF,__debug_info" ) {
334+ B->IDs .push_back (*AbbrevID);
335+ IsDebugInfoBlock = true ;
336+ }
337+
329338 // Encode Section.
330339 auto SectionIndex = CUB.getSectionIndex (Block.getSection ());
331340 if (!SectionIndex)
@@ -366,11 +375,10 @@ Expected<BlockRef> BlockRef::create(CompileUnitBuilder &CUB,
366375 return std::move (E);
367376 BlockSize = Content->size ();
368377 }
369-
370378 SmallString<1024 > EncodeContent;
371379 data::BlockData::encode (BlockSize, Block.getAlignment (),
372380 Block.getAlignmentOffset (), Content, Fixups,
373- EncodeContent);
381+ EncodeContent, IsDebugInfoBlock );
374382
375383 StringRef BlockData (EncodeContent);
376384 // Encode content first with size and data.
@@ -689,12 +697,37 @@ Error CompileUnitBuilder::createSection(const jitlink::Section &S) {
689697}
690698
691699Error CompileUnitBuilder::createBlock (const jitlink::Block &B) {
700+ if (B.getSection ().getName () == " __DWARF,__debug_abbrev" ||
701+ B.getSection ().getName () == " __DWARF,__debug_info" )
702+ return Error::success ();
703+ // Store the current idx. It is created in order so just add in the end.
704+ BlockIndexStarts.push_back (Indexes.size ());
705+ auto Block = BlockRef::create (*this , B);
706+ if (!Block)
707+ return Block.takeError ();
708+ commitNode (*Block);
709+ return Error::success ();
710+ }
711+
712+ Expected<cas::CASID>
713+ CompileUnitBuilder::createAbbrevBlock (const jitlink::Block &B) {
692714 // Store the current idx. It is created in order so just add in the end.
693715 BlockIndexStarts.push_back (Indexes.size ());
694716 auto Block = BlockRef::create (*this , B);
695717 if (!Block)
696718 return Block.takeError ();
697719 commitNode (*Block);
720+ return Block->getID ();
721+ }
722+
723+ Error CompileUnitBuilder::createInfoBlock (const jitlink::Block &B,
724+ cas::CASID *AbbrevID) {
725+ // Store the current idx. It is created in order so just add in the end.
726+ BlockIndexStarts.push_back (Indexes.size ());
727+ auto Block = BlockRef::create (*this , B, AbbrevID);
728+ if (!Block)
729+ return Block.takeError ();
730+ commitNode (*Block);
698731 return Error::success ();
699732}
700733
@@ -792,8 +825,24 @@ Expected<CompileUnitRef> CompileUnitRef::create(const ObjectFileSchema &Schema,
792825 llvm::sort (Builder.Blocks .begin () + PreviousSize, Builder.Blocks .end (),
793826 compareBlocksByAddress);
794827 };
795- for (const jitlink::Section &Section : G.sections ())
828+ SmallVector<const jitlink::Block *, 16 > AbbrevBlocks;
829+ SmallVector<const jitlink::Block *, 16 > InfoBlocks;
830+ for (const jitlink::Section &Section : G.sections ()) {
831+ if (Section.getName () == " __DWARF,__debug_abbrev" )
832+ AbbrevBlocks.append (Section.blocks ().begin (), Section.blocks ().end ());
833+ else if (Section.getName () == " __DWARF,__debug_info" )
834+ InfoBlocks.append (Section.blocks ().begin (), Section.blocks ().end ());
796835 appendBlocks (Section.blocks ());
836+ }
837+ // If the number of debug_abbrev blocks are 1, we assume that the
838+ // abbreviations could not be split up, and every debug_info compile unit
839+ // should have a reference to the same abbreviation.
840+ assert (AbbrevBlocks.size () == 1 ||
841+ AbbrevBlocks.size () == InfoBlocks.size () &&
842+ " The number of Abbreviation contributions should be equal to the "
843+ " number of Compile Units" );
844+ llvm::sort (InfoBlocks.begin (), InfoBlocks.end (), compareBlocksByAddress);
845+ llvm::sort (AbbrevBlocks.begin (), AbbrevBlocks.end (), compareBlocksByAddress);
797846
798847#ifndef NDEBUG
799848 for (auto *S : makeArrayRef (Symbols).slice (0 , DefinedSymbolsSize))
@@ -815,6 +864,22 @@ Expected<CompileUnitRef> CompileUnitRef::create(const ObjectFileSchema &Schema,
815864 return std::move (E);
816865 }
817866
867+ // Create BlockRefs for Abbrevs and Compile Units
868+ SmallVector<cas::CASID, 16 > AbbrevIDs;
869+ for (auto *B : AbbrevBlocks) {
870+ Expected<cas::CASID> ID = Builder.createAbbrevBlock (*B);
871+ if (!ID)
872+ return ID.takeError ();
873+ AbbrevIDs.push_back (*ID);
874+ }
875+ unsigned I = 0 ;
876+ for (auto *B : InfoBlocks) {
877+ if (auto E = Builder.createInfoBlock (
878+ *B, AbbrevIDs.size () == 1 ? &AbbrevIDs[0 ] : &AbbrevIDs[I]))
879+ return std::move (E);
880+ I++;
881+ }
882+
818883 auto B = Builder::startRootNode (Schema, KindString);
819884 if (!B)
820885 return B.takeError ();
0 commit comments