@@ -646,6 +646,15 @@ void DWARFRewriter::updateDebugInfo() {
646
646
647
647
} else {
648
648
LocListWritersByCU[CUIndex] = std::make_unique<DebugLocWriter>();
649
+ if (std::optional<uint64_t > DWOId = CU.getDWOId ()) {
650
+ assert (LegacyRangesWritersByCU.count (*DWOId) == 0 &&
651
+ " LegacyRangeLists writer for DWO unit already exists." );
652
+ auto LegacyRangesSectionWriterByCU =
653
+ std::make_unique<DebugRangesSectionWriter>();
654
+ LegacyRangesSectionWriterByCU->initSection (CU);
655
+ LegacyRangesWritersByCU[*DWOId] =
656
+ std::move (LegacyRangesSectionWriterByCU);
657
+ }
649
658
}
650
659
return LocListWritersByCU[CUIndex++].get ();
651
660
};
@@ -693,6 +702,7 @@ void DWARFRewriter::updateDebugInfo() {
693
702
if (Unit->getVersion () >= 5 ) {
694
703
TempRangesSectionWriter = RangeListsWritersByCU[*DWOId].get ();
695
704
} else {
705
+ TempRangesSectionWriter = LegacyRangesWritersByCU[*DWOId].get ();
696
706
RangesBase = RangesSectionWriter->getSectionOffset ();
697
707
setDwoRangesBase (*DWOId, *RangesBase);
698
708
}
@@ -1274,9 +1284,17 @@ void DWARFRewriter::updateDWARFObjectAddressRanges(
1274
1284
}
1275
1285
1276
1286
if (RangesBaseInfo) {
1277
- DIEBldr.replaceValue (&Die, RangesBaseInfo.getAttribute (),
1278
- RangesBaseInfo.getForm (),
1279
- DIEInteger (static_cast <uint32_t >(*RangesBase)));
1287
+ if (RangesBaseInfo.getAttribute () == dwarf::DW_AT_GNU_ranges_base) {
1288
+ auto RangesWriterIterator =
1289
+ LegacyRangesWritersByCU.find (*Unit.getDWOId ());
1290
+ assert (RangesWriterIterator != LegacyRangesWritersByCU.end () &&
1291
+ " RangesWriter does not exist for DWOId" );
1292
+ RangesWriterIterator->second ->setDie (&Die);
1293
+ } else {
1294
+ DIEBldr.replaceValue (&Die, RangesBaseInfo.getAttribute (),
1295
+ RangesBaseInfo.getForm (),
1296
+ DIEInteger (static_cast <uint32_t >(*RangesBase)));
1297
+ }
1280
1298
RangesBase = std::nullopt;
1281
1299
}
1282
1300
}
@@ -1294,20 +1312,12 @@ void DWARFRewriter::updateDWARFObjectAddressRanges(
1294
1312
RangesAttrInfo.getForm () == dwarf::DW_FORM_sec_offset)
1295
1313
NeedConverted = true ;
1296
1314
1297
- uint64_t CurRangeBase = 0 ;
1298
- if (Unit.isDWOUnit ()) {
1299
- if (std::optional<uint64_t > DWOId = Unit.getDWOId ())
1300
- CurRangeBase = getDwoRangesBase (*DWOId);
1301
- else
1302
- errs () << " BOLT-WARNING: [internal-dwarf-error]: DWOId is not found "
1303
- " for DWO Unit." ;
1304
- }
1305
1315
if (NeedConverted || RangesAttrInfo.getForm () == dwarf::DW_FORM_rnglistx)
1306
1316
DIEBldr.replaceValue (&Die, dwarf::DW_AT_ranges, dwarf::DW_FORM_rnglistx,
1307
1317
DIEInteger (DebugRangesOffset));
1308
1318
else
1309
1319
DIEBldr.replaceValue (&Die, dwarf::DW_AT_ranges, RangesAttrInfo.getForm (),
1310
- DIEInteger (DebugRangesOffset - CurRangeBase ));
1320
+ DIEInteger (DebugRangesOffset));
1311
1321
1312
1322
if (!RangesBase) {
1313
1323
if (LowPCAttrInfo &&
@@ -1324,15 +1334,21 @@ void DWARFRewriter::updateDWARFObjectAddressRanges(
1324
1334
1325
1335
// If we are at this point we are in the CU/Skeleton CU, and
1326
1336
// DW_AT_GNU_ranges_base or DW_AT_rnglists_base doesn't exist.
1327
- if (Unit.getVersion () <= 4 )
1337
+ if (Unit.getVersion () <= 4 ) {
1328
1338
DIEBldr.addValue (&Die, dwarf::DW_AT_GNU_ranges_base, dwarf::DW_FORM_data4,
1329
- DIEInteger (*RangesBase));
1330
- else if (Unit.getVersion () == 5 )
1339
+ DIEInteger (INT_MAX));
1340
+ auto RangesWriterIterator =
1341
+ LegacyRangesWritersByCU.find (*Unit.getDWOId ());
1342
+ assert (RangesWriterIterator != LegacyRangesWritersByCU.end () &&
1343
+ " RangesWriter does not exist for DWOId" );
1344
+ RangesWriterIterator->second ->setDie (&Die);
1345
+ } else if (Unit.getVersion () == 5 ) {
1331
1346
DIEBldr.addValue (&Die, dwarf::DW_AT_rnglists_base,
1332
1347
dwarf::DW_FORM_sec_offset, DIEInteger (*RangesBase));
1333
- else
1348
+ } else {
1334
1349
DIEBldr.addValue (&Die, dwarf::DW_AT_rnglists_base,
1335
1350
dwarf::DW_FORM_sec_offset, DIEInteger (*RangesBase));
1351
+ }
1336
1352
return ;
1337
1353
}
1338
1354
@@ -1611,6 +1627,30 @@ void DWARFRewriter::finalizeCompileUnits(DIEBuilder &DIEBlder,
1611
1627
DIEStreamer &Streamer,
1612
1628
CUOffsetMap &CUMap,
1613
1629
const std::list<DWARFUnit *> &CUs) {
1630
+ for (DWARFUnit *CU : CUs) {
1631
+ if (CU->getVersion () != 4 )
1632
+ continue ;
1633
+ std::optional<uint64_t > DWOId = CU->getDWOId ();
1634
+ if (!DWOId)
1635
+ continue ;
1636
+ auto RangesWriterIterator = LegacyRangesWritersByCU.find (*DWOId);
1637
+ assert (RangesWriterIterator != LegacyRangesWritersByCU.end () &&
1638
+ " RangesWriter does not exist for DWOId" );
1639
+ std::unique_ptr<DebugRangesSectionWriter> &LegacyRangesWriter =
1640
+ RangesWriterIterator->second ;
1641
+ std::optional<DIE *> Die = LegacyRangesWriter->getDie ();
1642
+ if (!Die || !Die.value ())
1643
+ continue ;
1644
+ DIEValue DvalGNUBase =
1645
+ Die.value ()->findAttribute (dwarf::DW_AT_GNU_ranges_base);
1646
+ assert (DvalGNUBase && " GNU_ranges_base attribute does not exist for DWOId" );
1647
+ DIEBlder.replaceValue (
1648
+ Die.value (), dwarf::DW_AT_GNU_ranges_base, DvalGNUBase.getForm (),
1649
+ DIEInteger (LegacyRangesSectionWriter->getSectionOffset ()));
1650
+ std::unique_ptr<DebugBufferVector> RangesWritersContents =
1651
+ LegacyRangesWriter->releaseBuffer ();
1652
+ LegacyRangesSectionWriter->appendToRangeBuffer (*RangesWritersContents);
1653
+ }
1614
1654
DIEBlder.generateAbbrevs ();
1615
1655
DIEBlder.finish ();
1616
1656
// generate debug_info and CUMap
@@ -2098,7 +2138,6 @@ void DWARFRewriter::convertToRangesPatchDebugInfo(
2098
2138
DWARFUnit &Unit, DIEBuilder &DIEBldr, DIE &Die,
2099
2139
uint64_t RangesSectionOffset, DIEValue &LowPCAttrInfo,
2100
2140
DIEValue &HighPCAttrInfo, std::optional<uint64_t > RangesBase) {
2101
- uint32_t BaseOffset = 0 ;
2102
2141
dwarf::Form LowForm = LowPCAttrInfo.getForm ();
2103
2142
dwarf::Attribute RangeBaseAttribute = dwarf::DW_AT_GNU_ranges_base;
2104
2143
dwarf::Form RangesForm = dwarf::DW_FORM_sec_offset;
@@ -2113,45 +2152,40 @@ void DWARFRewriter::convertToRangesPatchDebugInfo(
2113
2152
Die.getTag () == dwarf::DW_TAG_skeleton_unit;
2114
2153
if (!IsUnitDie)
2115
2154
DIEBldr.deleteValue (&Die, LowPCAttrInfo.getAttribute ());
2116
- // In DWARF4 for DW_AT_low_pc in binary DW_FORM_addr is used. In the DWO
2117
- // section DW_FORM_GNU_addr_index is used. So for if we are converting
2118
- // DW_AT_low_pc/DW_AT_high_pc and see DW_FORM_GNU_addr_index. We are
2119
- // converting in DWO section, and DW_AT_ranges [DW_FORM_sec_offset] is
2120
- // relative to DW_AT_GNU_ranges_base.
2121
- if (LowForm == dwarf::DW_FORM_GNU_addr_index) {
2122
- // Ranges are relative to DW_AT_GNU_ranges_base.
2123
- uint64_t CurRangeBase = 0 ;
2124
- if (std::optional<uint64_t > DWOId = Unit.getDWOId ()) {
2125
- CurRangeBase = getDwoRangesBase (*DWOId);
2126
- }
2127
- BaseOffset = CurRangeBase;
2128
- } else {
2129
- // In DWARF 5 we can have DW_AT_low_pc either as DW_FORM_addr, or
2130
- // DW_FORM_addrx. Former is when DW_AT_rnglists_base is present. Latter is
2131
- // when it's absent.
2132
- if (IsUnitDie) {
2133
- if (LowForm == dwarf::DW_FORM_addrx) {
2134
- const uint32_t Index = AddrWriter->getIndexFromAddress (0 , Unit);
2135
- DIEBldr.replaceValue (&Die, LowPCAttrInfo.getAttribute (),
2136
- LowPCAttrInfo.getForm (), DIEInteger (Index));
2137
- } else {
2138
- DIEBldr.replaceValue (&Die, LowPCAttrInfo.getAttribute (),
2139
- LowPCAttrInfo.getForm (), DIEInteger (0 ));
2140
- }
2155
+
2156
+ // In DWARF 5 we can have DW_AT_low_pc either as DW_FORM_addr, or
2157
+ // DW_FORM_addrx. Former is when DW_AT_rnglists_base is present. Latter is
2158
+ // when it's absent.
2159
+ if (IsUnitDie) {
2160
+ if (LowForm == dwarf::DW_FORM_addrx) {
2161
+ const uint32_t Index = AddrWriter->getIndexFromAddress (0 , Unit);
2162
+ DIEBldr.replaceValue (&Die, LowPCAttrInfo.getAttribute (),
2163
+ LowPCAttrInfo.getForm (), DIEInteger (Index));
2164
+ } else {
2165
+ DIEBldr.replaceValue (&Die, LowPCAttrInfo.getAttribute (),
2166
+ LowPCAttrInfo.getForm (), DIEInteger (0 ));
2141
2167
}
2142
- // Original CU didn't have DW_AT_*_base. We converted it's children (or
2143
- // dwo), so need to insert it into CU.
2144
- if (RangesBase)
2168
+ }
2169
+ // Original CU didn't have DW_AT_*_base. We converted it's children (or
2170
+ // dwo), so need to insert it into CU.
2171
+ if (RangesBase) {
2172
+ if (Unit.getVersion () >= 5 ) {
2145
2173
DIEBldr.addValue (&Die, RangeBaseAttribute, dwarf::DW_FORM_sec_offset,
2146
2174
DIEInteger (*RangesBase));
2175
+ } else {
2176
+ DIEBldr.addValue (&Die, RangeBaseAttribute, dwarf::DW_FORM_sec_offset,
2177
+ DIEInteger (INT_MAX));
2178
+ auto RangesWriterIterator =
2179
+ LegacyRangesWritersByCU.find (*Unit.getDWOId ());
2180
+ assert (RangesWriterIterator != LegacyRangesWritersByCU.end () &&
2181
+ " RangesWriter does not exist for DWOId" );
2182
+ RangesWriterIterator->second ->setDie (&Die);
2183
+ }
2147
2184
}
2148
2185
2149
- uint64_t RangeAttrVal = RangesSectionOffset - BaseOffset;
2150
- if (Unit.getVersion () >= 5 )
2151
- RangeAttrVal = RangesSectionOffset;
2152
2186
// HighPC was conveted into DW_AT_ranges.
2153
2187
// For DWARF5 we only access ranges through index.
2154
2188
2155
2189
DIEBldr.replaceValue (&Die, HighPCAttrInfo.getAttribute (), dwarf::DW_AT_ranges,
2156
- RangesForm, DIEInteger (RangeAttrVal ));
2190
+ RangesForm, DIEInteger (RangesSectionOffset ));
2157
2191
}
0 commit comments