1 //===- bolt/Core/DebugData.cpp - Debugging information handling -----------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements functions and classes for handling debug info. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "bolt/Core/DebugData.h" 14 #include "bolt/Core/BinaryContext.h" 15 #include "bolt/Rewrite/RewriteInstance.h" 16 #include "bolt/Utils/Utils.h" 17 #include "llvm/DebugInfo/DWARF/DWARFCompileUnit.h" 18 #include "llvm/DebugInfo/DWARF/DWARFDebugAbbrev.h" 19 #include "llvm/DebugInfo/DWARF/DWARFDebugAddr.h" 20 #include "llvm/MC/MCAssembler.h" 21 #include "llvm/MC/MCContext.h" 22 #include "llvm/MC/MCObjectStreamer.h" 23 #include "llvm/Support/CommandLine.h" 24 #include "llvm/Support/EndianStream.h" 25 #include "llvm/Support/LEB128.h" 26 #include "llvm/Support/SHA1.h" 27 #include <algorithm> 28 #include <cassert> 29 #include <cstdint> 30 #include <limits> 31 #include <unordered_map> 32 #include <vector> 33 34 #define DEBUG_TYPE "bolt-debug-info" 35 36 namespace opts { 37 extern llvm::cl::opt<unsigned> Verbosity; 38 } // namespace opts 39 40 namespace llvm { 41 class MCSymbol; 42 43 namespace bolt { 44 45 /// Finds attributes FormValue and Offset. 46 /// 47 /// \param DIE die to look up in. 48 /// \param Index the attribute index to extract. 49 /// \return an optional AttrInfo with DWARFFormValue and Offset. 50 Optional<AttrInfo> 51 findAttributeInfo(const DWARFDie DIE, 52 const DWARFAbbreviationDeclaration *AbbrevDecl, 53 uint32_t Index) { 54 const DWARFUnit &U = *DIE.getDwarfUnit(); 55 uint64_t Offset = 56 AbbrevDecl->getAttributeOffsetFromIndex(Index, DIE.getOffset(), U); 57 Optional<DWARFFormValue> Value = 58 AbbrevDecl->getAttributeValueFromOffset(Index, Offset, U); 59 if (!Value) 60 return None; 61 // AttributeSpec 62 const DWARFAbbreviationDeclaration::AttributeSpec *AttrVal = 63 AbbrevDecl->attributes().begin() + Index; 64 uint32_t ValSize = 0; 65 Optional<int64_t> ValSizeOpt = AttrVal->getByteSize(U); 66 if (ValSizeOpt) { 67 ValSize = static_cast<uint32_t>(*ValSizeOpt); 68 } else { 69 DWARFDataExtractor DebugInfoData = U.getDebugInfoExtractor(); 70 uint64_t NewOffset = Offset; 71 DWARFFormValue::skipValue(Value->getForm(), DebugInfoData, &NewOffset, 72 U.getFormParams()); 73 // This includes entire size of the entry, which might not be just the 74 // encoding part. For example for DW_AT_loc it will include expression 75 // location. 76 ValSize = NewOffset - Offset; 77 } 78 79 return AttrInfo{*Value, Offset, ValSize}; 80 } 81 82 const DebugLineTableRowRef DebugLineTableRowRef::NULL_ROW{0, 0}; 83 84 namespace { 85 86 LLVM_ATTRIBUTE_UNUSED 87 static void printLE64(const std::string &S) { 88 for (uint32_t I = 0, Size = S.size(); I < Size; ++I) { 89 errs() << Twine::utohexstr(S[I]); 90 errs() << Twine::utohexstr((int8_t)S[I]); 91 } 92 errs() << "\n"; 93 } 94 95 // Writes address ranges to Writer as pairs of 64-bit (address, size). 96 // If RelativeRange is true, assumes the address range to be written must be of 97 // the form (begin address, range size), otherwise (begin address, end address). 98 // Terminates the list by writing a pair of two zeroes. 99 // Returns the number of written bytes. 100 uint64_t writeAddressRanges(raw_svector_ostream &Stream, 101 const DebugAddressRangesVector &AddressRanges, 102 const bool WriteRelativeRanges = false) { 103 for (const DebugAddressRange &Range : AddressRanges) { 104 support::endian::write(Stream, Range.LowPC, support::little); 105 support::endian::write( 106 Stream, WriteRelativeRanges ? Range.HighPC - Range.LowPC : Range.HighPC, 107 support::little); 108 } 109 // Finish with 0 entries. 110 support::endian::write(Stream, 0ULL, support::little); 111 support::endian::write(Stream, 0ULL, support::little); 112 return AddressRanges.size() * 16 + 16; 113 } 114 115 } // namespace 116 117 DebugRangesSectionWriter::DebugRangesSectionWriter() { 118 RangesBuffer = std::make_unique<DebugBufferVector>(); 119 RangesStream = std::make_unique<raw_svector_ostream>(*RangesBuffer); 120 121 // Add an empty range as the first entry; 122 SectionOffset += 123 writeAddressRanges(*RangesStream.get(), DebugAddressRangesVector{}); 124 Kind = RangesWriterKind::DebugRangesWriter; 125 } 126 127 uint64_t DebugRangesSectionWriter::addRanges( 128 DebugAddressRangesVector &&Ranges, 129 std::map<DebugAddressRangesVector, uint64_t> &CachedRanges) { 130 if (Ranges.empty()) 131 return getEmptyRangesOffset(); 132 133 const auto RI = CachedRanges.find(Ranges); 134 if (RI != CachedRanges.end()) 135 return RI->second; 136 137 const uint64_t EntryOffset = addRanges(Ranges); 138 CachedRanges.emplace(std::move(Ranges), EntryOffset); 139 140 return EntryOffset; 141 } 142 143 uint64_t 144 DebugRangesSectionWriter::addRanges(const DebugAddressRangesVector &Ranges) { 145 if (Ranges.empty()) 146 return getEmptyRangesOffset(); 147 148 // Reading the SectionOffset and updating it should be atomic to guarantee 149 // unique and correct offsets in patches. 150 std::lock_guard<std::mutex> Lock(WriterMutex); 151 const uint32_t EntryOffset = SectionOffset; 152 SectionOffset += writeAddressRanges(*RangesStream.get(), Ranges); 153 154 return EntryOffset; 155 } 156 157 uint64_t DebugRangesSectionWriter::getSectionOffset() { 158 std::lock_guard<std::mutex> Lock(WriterMutex); 159 return SectionOffset; 160 } 161 162 DebugAddrWriter *DebugRangeListsSectionWriter::AddrWriter = nullptr; 163 164 uint64_t DebugRangeListsSectionWriter::addRanges( 165 DebugAddressRangesVector &&Ranges, 166 std::map<DebugAddressRangesVector, uint64_t> &CachedRanges) { 167 return addRanges(Ranges); 168 } 169 170 struct LocListsRangelistsHeader { 171 UnitLengthType UnitLength; // Size of loclist entris section, not including 172 // size of header. 173 VersionType Version; 174 AddressSizeType AddressSize; 175 SegmentSelectorType SegmentSelector; 176 OffsetEntryCountType OffsetEntryCount; 177 }; 178 179 static std::unique_ptr<DebugBufferVector> 180 getDWARF5Header(const LocListsRangelistsHeader &Header) { 181 std::unique_ptr<DebugBufferVector> HeaderBuffer = 182 std::make_unique<DebugBufferVector>(); 183 std::unique_ptr<raw_svector_ostream> HeaderStream = 184 std::make_unique<raw_svector_ostream>(*HeaderBuffer); 185 186 // 7.29 length of the set of entries for this compilation unit, not including 187 // the length field itself 188 const uint32_t HeaderSize = 189 getDWARF5RngListLocListHeaderSize() - sizeof(UnitLengthType); 190 191 support::endian::write(*HeaderStream, Header.UnitLength + HeaderSize, 192 support::little); 193 support::endian::write(*HeaderStream, Header.Version, support::little); 194 support::endian::write(*HeaderStream, Header.AddressSize, support::little); 195 support::endian::write(*HeaderStream, Header.SegmentSelector, 196 support::little); 197 support::endian::write(*HeaderStream, Header.OffsetEntryCount, 198 support::little); 199 return HeaderBuffer; 200 } 201 202 uint64_t DebugRangeListsSectionWriter::addRanges( 203 const DebugAddressRangesVector &Ranges) { 204 std::lock_guard<std::mutex> Lock(WriterMutex); 205 206 RangeEntries.push_back(CurrentOffset); 207 for (const DebugAddressRange &Range : Ranges) { 208 support::endian::write(*CUBodyStream, 209 static_cast<uint8_t>(dwarf::DW_RLE_startx_length), 210 support::little); 211 uint32_t Index = AddrWriter->getIndexFromAddress(Range.LowPC, *CU); 212 encodeULEB128(Index, *CUBodyStream); 213 encodeULEB128(Range.HighPC - Range.LowPC, *CUBodyStream); 214 } 215 support::endian::write(*CUBodyStream, 216 static_cast<uint8_t>(dwarf::DW_RLE_end_of_list), 217 support::little); 218 CurrentOffset = CUBodyBuffer->size(); 219 return RangeEntries.size() - 1; 220 } 221 222 void DebugRangeListsSectionWriter::finalizeSection() { 223 std::unique_ptr<DebugBufferVector> CUArrayBuffer = 224 std::make_unique<DebugBufferVector>(); 225 std::unique_ptr<raw_svector_ostream> CUArrayStream = 226 std::make_unique<raw_svector_ostream>(*CUArrayBuffer); 227 constexpr uint32_t SizeOfArrayEntry = 4; 228 const uint32_t SizeOfArraySection = RangeEntries.size() * SizeOfArrayEntry; 229 for (uint32_t Offset : RangeEntries) 230 support::endian::write(*CUArrayStream, Offset + SizeOfArraySection, 231 support::little); 232 233 std::unique_ptr<DebugBufferVector> Header = getDWARF5Header( 234 {static_cast<uint32_t>(SizeOfArraySection + CUBodyBuffer.get()->size()), 235 5, 8, 0, static_cast<uint32_t>(RangeEntries.size())}); 236 *RangesStream << *Header; 237 *RangesStream << *CUArrayBuffer; 238 *RangesStream << *CUBodyBuffer; 239 SectionOffset = RangesBuffer->size(); 240 } 241 242 void DebugRangeListsSectionWriter::initSection(DWARFUnit &Unit) { 243 CUBodyBuffer = std::make_unique<DebugBufferVector>(); 244 CUBodyStream = std::make_unique<raw_svector_ostream>(*CUBodyBuffer); 245 RangeEntries.clear(); 246 CurrentOffset = 0; 247 CU = &Unit; 248 } 249 250 void DebugARangesSectionWriter::addCURanges(uint64_t CUOffset, 251 DebugAddressRangesVector &&Ranges) { 252 std::lock_guard<std::mutex> Lock(CUAddressRangesMutex); 253 CUAddressRanges.emplace(CUOffset, std::move(Ranges)); 254 } 255 256 void DebugARangesSectionWriter::writeARangesSection( 257 raw_svector_ostream &RangesStream, const CUOffsetMap &CUMap) const { 258 // For reference on the format of the .debug_aranges section, see the DWARF4 259 // specification, section 6.1.4 Lookup by Address 260 // http://www.dwarfstd.org/doc/DWARF4.pdf 261 for (const auto &CUOffsetAddressRangesPair : CUAddressRanges) { 262 const uint64_t Offset = CUOffsetAddressRangesPair.first; 263 const DebugAddressRangesVector &AddressRanges = 264 CUOffsetAddressRangesPair.second; 265 266 // Emit header. 267 268 // Size of this set: 8 (size of the header) + 4 (padding after header) 269 // + 2*sizeof(uint64_t) bytes for each of the ranges, plus an extra 270 // pair of uint64_t's for the terminating, zero-length range. 271 // Does not include size field itself. 272 uint32_t Size = 8 + 4 + 2 * sizeof(uint64_t) * (AddressRanges.size() + 1); 273 274 // Header field #1: set size. 275 support::endian::write(RangesStream, Size, support::little); 276 277 // Header field #2: version number, 2 as per the specification. 278 support::endian::write(RangesStream, static_cast<uint16_t>(2), 279 support::little); 280 281 assert(CUMap.count(Offset) && "Original CU offset is not found in CU Map"); 282 // Header field #3: debug info offset of the correspondent compile unit. 283 support::endian::write( 284 RangesStream, static_cast<uint32_t>(CUMap.find(Offset)->second.Offset), 285 support::little); 286 287 // Header field #4: address size. 288 // 8 since we only write ELF64 binaries for now. 289 RangesStream << char(8); 290 291 // Header field #5: segment size of target architecture. 292 RangesStream << char(0); 293 294 // Padding before address table - 4 bytes in the 64-bit-pointer case. 295 support::endian::write(RangesStream, static_cast<uint32_t>(0), 296 support::little); 297 298 writeAddressRanges(RangesStream, AddressRanges, true); 299 } 300 } 301 302 DebugAddrWriter::DebugAddrWriter(BinaryContext *Bc) { BC = Bc; } 303 304 void DebugAddrWriter::AddressForDWOCU::dump() { 305 std::vector<IndexAddressPair> SortedMap(indexToAddressBegin(), 306 indexToAdddessEnd()); 307 // Sorting address in increasing order of indices. 308 std::sort(SortedMap.begin(), SortedMap.end(), 309 [](const IndexAddressPair &A, const IndexAddressPair &B) { 310 return A.first < B.first; 311 }); 312 for (auto &Pair : SortedMap) 313 dbgs() << Twine::utohexstr(Pair.second) << "\t" << Pair.first << "\n"; 314 } 315 uint32_t DebugAddrWriter::getIndexFromAddress(uint64_t Address, DWARFUnit &CU) { 316 std::lock_guard<std::mutex> Lock(WriterMutex); 317 const uint64_t CUID = getCUID(CU); 318 if (!AddressMaps.count(CUID)) 319 AddressMaps[CUID] = AddressForDWOCU(); 320 321 AddressForDWOCU &Map = AddressMaps[CUID]; 322 auto Entry = Map.find(Address); 323 if (Entry == Map.end()) { 324 auto Index = Map.getNextIndex(); 325 Entry = Map.insert(Address, Index).first; 326 } 327 return Entry->second; 328 } 329 330 // Case1) Address is not in map insert in to AddresToIndex and IndexToAddres 331 // Case2) Address is in the map but Index is higher or equal. Need to update 332 // IndexToAddrss. Case3) Address is in the map but Index is lower. Need to 333 // update AddressToIndex and IndexToAddress 334 void DebugAddrWriter::addIndexAddress(uint64_t Address, uint32_t Index, 335 DWARFUnit &CU) { 336 std::lock_guard<std::mutex> Lock(WriterMutex); 337 const uint64_t CUID = getCUID(CU); 338 AddressForDWOCU &Map = AddressMaps[CUID]; 339 auto Entry = Map.find(Address); 340 if (Entry != Map.end()) { 341 if (Entry->second > Index) 342 Map.updateAddressToIndex(Address, Index); 343 Map.updateIndexToAddrss(Address, Index); 344 } else { 345 Map.insert(Address, Index); 346 } 347 } 348 349 AddressSectionBuffer DebugAddrWriter::finalize() { 350 // Need to layout all sections within .debug_addr 351 // Within each section sort Address by index. 352 AddressSectionBuffer Buffer; 353 raw_svector_ostream AddressStream(Buffer); 354 for (std::unique_ptr<DWARFUnit> &CU : BC->DwCtx->compile_units()) { 355 // Handling the case wehre debug information is a mix of Debug fission and 356 // monolitic. 357 if (!CU->getDWOId()) 358 continue; 359 const uint64_t CUID = getCUID(*CU.get()); 360 auto AM = AddressMaps.find(CUID); 361 // Adding to map even if it did not contribute to .debug_addr. 362 // The Skeleton CU might still have DW_AT_GNU_addr_base. 363 DWOIdToOffsetMap[CUID] = Buffer.size(); 364 // If does not exist this CUs DWO section didn't contribute to .debug_addr. 365 if (AM == AddressMaps.end()) 366 continue; 367 std::vector<IndexAddressPair> SortedMap(AM->second.indexToAddressBegin(), 368 AM->second.indexToAdddessEnd()); 369 // Sorting address in increasing order of indices. 370 std::sort(SortedMap.begin(), SortedMap.end(), 371 [](const IndexAddressPair &A, const IndexAddressPair &B) { 372 return A.first < B.first; 373 }); 374 375 uint8_t AddrSize = CU->getAddressByteSize(); 376 uint32_t Counter = 0; 377 auto WriteAddress = [&](uint64_t Address) -> void { 378 ++Counter; 379 switch (AddrSize) { 380 default: 381 assert(false && "Address Size is invalid."); 382 break; 383 case 4: 384 support::endian::write(AddressStream, static_cast<uint32_t>(Address), 385 support::little); 386 break; 387 case 8: 388 support::endian::write(AddressStream, Address, support::little); 389 break; 390 } 391 }; 392 393 for (const IndexAddressPair &Val : SortedMap) { 394 while (Val.first > Counter) 395 WriteAddress(0); 396 WriteAddress(Val.second); 397 } 398 } 399 400 return Buffer; 401 } 402 AddressSectionBuffer DebugAddrWriterDwarf5::finalize() { 403 // Need to layout all sections within .debug_addr 404 // Within each section sort Address by index. 405 AddressSectionBuffer Buffer; 406 raw_svector_ostream AddressStream(Buffer); 407 const endianness Endian = 408 BC->DwCtx->isLittleEndian() ? support::little : support::big; 409 const DWARFSection &AddrSec = BC->DwCtx->getDWARFObj().getAddrSection(); 410 DWARFDataExtractor AddrData(BC->DwCtx->getDWARFObj(), AddrSec, Endian, 0); 411 DWARFDebugAddrTable AddrTable; 412 DIDumpOptions DumpOpts; 413 constexpr uint32_t HeaderSize = 8; 414 for (std::unique_ptr<DWARFUnit> &CU : BC->DwCtx->compile_units()) { 415 const uint64_t CUID = getCUID(*CU.get()); 416 const uint8_t AddrSize = CU->getAddressByteSize(); 417 auto AMIter = AddressMaps.find(CUID); 418 // A case where CU has entry in .debug_addr, but we don't modify addresses 419 // for it. 420 if (AMIter == AddressMaps.end()) { 421 AMIter = AddressMaps.insert({CUID, AddressForDWOCU()}).first; 422 Optional<uint64_t> BaseOffset = CU->getAddrOffsetSectionBase(); 423 if (!BaseOffset) 424 continue; 425 // Address base offset is to the first entry. 426 // The size of header is 8 bytes. 427 uint64_t Offset = *BaseOffset - HeaderSize; 428 if (Error Err = AddrTable.extract(AddrData, &Offset, 5, AddrSize, 429 DumpOpts.WarningHandler)) { 430 DumpOpts.RecoverableErrorHandler(std::move(Err)); 431 continue; 432 } 433 uint32_t Index = 0; 434 for (uint64_t Addr : AddrTable.getAddressEntries()) 435 AMIter->second.insert(Addr, Index++); 436 } 437 438 DWOIdToOffsetMap[CUID] = Buffer.size() + HeaderSize; 439 440 std::vector<IndexAddressPair> SortedMap( 441 AMIter->second.indexToAddressBegin(), 442 AMIter->second.indexToAdddessEnd()); 443 // Sorting address in increasing order of indices. 444 std::sort(SortedMap.begin(), SortedMap.end(), 445 [](const IndexAddressPair &A, const IndexAddressPair &B) { 446 return A.first < B.first; 447 }); 448 // Writing out Header 449 const uint32_t Length = SortedMap.size() * AddrSize + 4; 450 support::endian::write(AddressStream, Length, Endian); 451 support::endian::write(AddressStream, static_cast<uint16_t>(5), Endian); 452 support::endian::write(AddressStream, static_cast<uint8_t>(AddrSize), 453 Endian); 454 support::endian::write(AddressStream, static_cast<uint8_t>(0), Endian); 455 456 uint32_t Counter = 0; 457 auto writeAddress = [&](uint64_t Address) -> void { 458 ++Counter; 459 switch (AddrSize) { 460 default: 461 llvm_unreachable("Address Size is invalid."); 462 break; 463 case 4: 464 support::endian::write(AddressStream, static_cast<uint32_t>(Address), 465 Endian); 466 break; 467 case 8: 468 support::endian::write(AddressStream, Address, Endian); 469 break; 470 } 471 }; 472 473 for (const IndexAddressPair &Val : SortedMap) { 474 while (Val.first > Counter) 475 writeAddress(0); 476 writeAddress(Val.second); 477 } 478 } 479 480 return Buffer; 481 } 482 483 uint64_t DebugAddrWriter::getOffset(DWARFUnit &Unit) { 484 const uint64_t CUID = getCUID(Unit); 485 assert(CUID && "Can't get offset, not a skeleton CU."); 486 auto Iter = DWOIdToOffsetMap.find(CUID); 487 assert(Iter != DWOIdToOffsetMap.end() && 488 "Offset in to.debug_addr was not found for DWO ID."); 489 return Iter->second; 490 } 491 492 uint64_t DebugAddrWriterDwarf5::getOffset(DWARFUnit &Unit) { 493 auto Iter = DWOIdToOffsetMap.find(getCUID(Unit)); 494 assert(Iter != DWOIdToOffsetMap.end() && 495 "Offset in to.debug_addr was not found for CU ID."); 496 return Iter->second; 497 } 498 499 DebugLocWriter::DebugLocWriter(BinaryContext *BC) { 500 LocBuffer = std::make_unique<DebugBufferVector>(); 501 LocStream = std::make_unique<raw_svector_ostream>(*LocBuffer); 502 } 503 504 void DebugLocWriter::addList(uint64_t AttrOffset, uint32_t LocListIndex, 505 DebugLocationsVector &&LocList) { 506 if (LocList.empty()) { 507 EmptyAttrLists.push_back(AttrOffset); 508 return; 509 } 510 // Since there is a separate DebugLocWriter for each thread, 511 // we don't need a lock to read the SectionOffset and update it. 512 const uint32_t EntryOffset = SectionOffset; 513 514 for (const DebugLocationEntry &Entry : LocList) { 515 support::endian::write(*LocStream, static_cast<uint64_t>(Entry.LowPC), 516 support::little); 517 support::endian::write(*LocStream, static_cast<uint64_t>(Entry.HighPC), 518 support::little); 519 support::endian::write(*LocStream, static_cast<uint16_t>(Entry.Expr.size()), 520 support::little); 521 *LocStream << StringRef(reinterpret_cast<const char *>(Entry.Expr.data()), 522 Entry.Expr.size()); 523 SectionOffset += 2 * 8 + 2 + Entry.Expr.size(); 524 } 525 LocStream->write_zeros(16); 526 SectionOffset += 16; 527 LocListDebugInfoPatches.push_back({AttrOffset, EntryOffset}); 528 } 529 530 void DebugLoclistWriter::addList(uint64_t AttrOffset, uint32_t LocListIndex, 531 DebugLocationsVector &&LocList) { 532 Patches.push_back({AttrOffset, LocListIndex, std::move(LocList)}); 533 } 534 535 std::unique_ptr<DebugBufferVector> DebugLocWriter::getBuffer() { 536 return std::move(LocBuffer); 537 } 538 539 // DWARF 4: 2.6.2 540 void DebugLocWriter::finalize(uint64_t SectionOffset, 541 SimpleBinaryPatcher &DebugInfoPatcher) { 542 for (const auto LocListDebugInfoPatchType : LocListDebugInfoPatches) { 543 uint64_t Offset = SectionOffset + LocListDebugInfoPatchType.LocListOffset; 544 DebugInfoPatcher.addLE32Patch(LocListDebugInfoPatchType.DebugInfoAttrOffset, 545 Offset); 546 } 547 548 for (uint64_t DebugInfoAttrOffset : EmptyAttrLists) 549 DebugInfoPatcher.addLE32Patch(DebugInfoAttrOffset, 550 DebugLocWriter::EmptyListOffset); 551 } 552 553 static void writeEmptyListDwarf5(raw_svector_ostream &Stream) { 554 support::endian::write(Stream, static_cast<uint32_t>(4), support::little); 555 support::endian::write(Stream, static_cast<uint8_t>(dwarf::DW_LLE_start_end), 556 support::little); 557 558 const char Zeroes[16] = {0}; 559 Stream << StringRef(Zeroes, 16); 560 encodeULEB128(0, Stream); 561 support::endian::write( 562 Stream, static_cast<uint8_t>(dwarf::DW_LLE_end_of_list), support::little); 563 } 564 565 void DebugLoclistWriter::finalizeDWARF5(uint64_t SectionOffset, 566 SimpleBinaryPatcher &DebugInfoPatcher) { 567 568 std::unique_ptr<DebugBufferVector> LocArrayBuffer = 569 std::make_unique<DebugBufferVector>(); 570 std::unique_ptr<raw_svector_ostream> LocArrayStream = 571 std::make_unique<raw_svector_ostream>(*LocArrayBuffer); 572 std::unique_ptr<DebugBufferVector> LocBodyBuffer = 573 std::make_unique<DebugBufferVector>(); 574 std::unique_ptr<raw_svector_ostream> LocBodyStream = 575 std::make_unique<raw_svector_ostream>(*LocBodyBuffer); 576 577 const uint32_t SizeOfArraySection = Patches.size() * sizeof(uint32_t); 578 std::sort(Patches.begin(), Patches.end(), 579 [](const LocPatch &P1, const LocPatch &P2) -> bool { 580 return P1.Index < P2.Index; 581 }); 582 583 if (LocListsBaseAttrOffset != InvalidLocListsBaseAttrOffset) 584 DebugInfoPatcher.addLE32Patch(LocListsBaseAttrOffset, 585 SectionOffset + 586 getDWARF5RngListLocListHeaderSize()); 587 588 uint32_t Index{0}; 589 for (LocPatch &Patch : Patches) { 590 const uint32_t EntryOffset = LocBodyBuffer->size(); 591 if (Patch.LocList.empty()) { 592 if (Patch.Index == DebugLoclistWriter::InvalidIndex) 593 DebugInfoPatcher.addLE32Patch(Patch.AttrOffset, EntryOffset); 594 595 writeEmptyListDwarf5(*LocBodyStream); 596 continue; 597 } 598 599 assert(Patch.Index == DebugLoclistWriter::InvalidIndex || 600 Patch.Index == Index++ && "Gap in LocList Index Array."); 601 (void)Index; 602 603 std::vector<uint64_t> OffsetsArray; 604 for (const DebugLocationEntry &Entry : Patch.LocList) { 605 support::endian::write(*LocBodyStream, 606 static_cast<uint8_t>(dwarf::DW_LLE_startx_length), 607 support::little); 608 const uint32_t Index = AddrWriter->getIndexFromAddress(Entry.LowPC, CU); 609 encodeULEB128(Index, *LocBodyStream); 610 encodeULEB128(Entry.HighPC - Entry.LowPC, *LocBodyStream); 611 encodeULEB128(Entry.Expr.size(), *LocBodyStream); 612 *LocBodyStream << StringRef( 613 reinterpret_cast<const char *>(Entry.Expr.data()), Entry.Expr.size()); 614 } 615 support::endian::write(*LocBodyStream, 616 static_cast<uint8_t>(dwarf::DW_LLE_end_of_list), 617 support::little); 618 619 // Write out IndexArray 620 support::endian::write( 621 *LocArrayStream, 622 static_cast<uint32_t>(SizeOfArraySection + EntryOffset), 623 support::little); 624 // Don't need to patch Index since we are re-using them. 625 if (Patch.Index == DebugLoclistWriter::InvalidIndex) 626 DebugInfoPatcher.addLE32Patch(Patch.AttrOffset, EntryOffset); 627 clearList(Patch.LocList); 628 } 629 if (!Patches.empty()) { 630 std::unique_ptr<DebugBufferVector> Header = 631 getDWARF5Header({static_cast<uint32_t>(SizeOfArraySection + 632 LocBodyBuffer.get()->size()), 633 5, 8, 0, static_cast<uint32_t>(Patches.size())}); 634 *LocStream << *Header; 635 *LocStream << *LocArrayBuffer; 636 *LocStream << *LocBodyBuffer; 637 } 638 clearList(Patches); 639 } 640 641 void DebugLoclistWriter::finalizeDWARFLegacy( 642 uint64_t SectionOffset, SimpleBinaryPatcher &DebugInfoPatcher) { 643 for (LocPatch &Patch : Patches) { 644 if (Patch.LocList.empty()) { 645 DebugInfoPatcher.addLE32Patch(Patch.AttrOffset, 646 DebugLocWriter::EmptyListOffset); 647 continue; 648 } 649 const uint32_t EntryOffset = LocBuffer->size(); 650 for (const DebugLocationEntry &Entry : Patch.LocList) { 651 support::endian::write(*LocStream, 652 static_cast<uint8_t>(dwarf::DW_LLE_startx_length), 653 support::little); 654 const uint32_t Index = AddrWriter->getIndexFromAddress(Entry.LowPC, CU); 655 encodeULEB128(Index, *LocStream); 656 657 // TODO: Support DWARF5 658 support::endian::write(*LocStream, 659 static_cast<uint32_t>(Entry.HighPC - Entry.LowPC), 660 support::little); 661 support::endian::write(*LocStream, 662 static_cast<uint16_t>(Entry.Expr.size()), 663 support::little); 664 *LocStream << StringRef(reinterpret_cast<const char *>(Entry.Expr.data()), 665 Entry.Expr.size()); 666 } 667 support::endian::write(*LocStream, 668 static_cast<uint8_t>(dwarf::DW_LLE_end_of_list), 669 support::little); 670 DebugInfoPatcher.addLE32Patch(Patch.AttrOffset, EntryOffset); 671 clearList(Patch.LocList); 672 } 673 clearList(Patches); 674 } 675 676 void DebugLoclistWriter::finalize(uint64_t SectionOffset, 677 SimpleBinaryPatcher &DebugInfoPatcher) { 678 if (DwarfVersion < 5) 679 finalizeDWARFLegacy(SectionOffset, DebugInfoPatcher); 680 else 681 finalizeDWARF5(SectionOffset, DebugInfoPatcher); 682 } 683 684 DebugAddrWriter *DebugLoclistWriter::AddrWriter = nullptr; 685 686 void DebugInfoBinaryPatcher::addUnitBaseOffsetLabel(uint64_t Offset) { 687 Offset -= DWPUnitOffset; 688 std::lock_guard<std::mutex> Lock(WriterMutex); 689 DebugPatches.emplace_back(new DWARFUnitOffsetBaseLabel(Offset)); 690 } 691 692 void DebugInfoBinaryPatcher::addDestinationReferenceLabel(uint64_t Offset) { 693 Offset -= DWPUnitOffset; 694 std::lock_guard<std::mutex> Lock(WriterMutex); 695 auto RetVal = DestinationLabels.insert(Offset); 696 if (!RetVal.second) 697 return; 698 699 DebugPatches.emplace_back(new DestinationReferenceLabel(Offset)); 700 } 701 702 static std::string encodeLE(size_t ByteSize, uint64_t NewValue) { 703 std::string LE64(ByteSize, 0); 704 for (size_t I = 0; I < ByteSize; ++I) { 705 LE64[I] = NewValue & 0xff; 706 NewValue >>= 8; 707 } 708 return LE64; 709 } 710 711 void DebugInfoBinaryPatcher::insertNewEntry(const DWARFDie &DIE, 712 uint32_t Value) { 713 std::string StrValue = encodeLE(4, Value); 714 insertNewEntry(DIE, std::move(StrValue)); 715 } 716 717 void DebugInfoBinaryPatcher::insertNewEntry(const DWARFDie &DIE, 718 std::string &&Value) { 719 const DWARFAbbreviationDeclaration *AbbrevDecl = 720 DIE.getAbbreviationDeclarationPtr(); 721 722 // In case this DIE has no attributes. 723 uint32_t Offset = DIE.getOffset() + 1; 724 size_t NumOfAttributes = AbbrevDecl->getNumAttributes(); 725 if (NumOfAttributes) { 726 Optional<AttrInfo> Val = 727 findAttributeInfo(DIE, AbbrevDecl, NumOfAttributes - 1); 728 assert(Val && "Invalid Value."); 729 730 Offset = Val->Offset + Val->Size - DWPUnitOffset; 731 } 732 std::lock_guard<std::mutex> Lock(WriterMutex); 733 DebugPatches.emplace_back(new NewDebugEntry(Offset, std::move(Value))); 734 } 735 736 void DebugInfoBinaryPatcher::addReferenceToPatch(uint64_t Offset, 737 uint32_t DestinationOffset, 738 uint32_t OldValueSize, 739 dwarf::Form Form) { 740 Offset -= DWPUnitOffset; 741 DestinationOffset -= DWPUnitOffset; 742 std::lock_guard<std::mutex> Lock(WriterMutex); 743 DebugPatches.emplace_back( 744 new DebugPatchReference(Offset, OldValueSize, DestinationOffset, Form)); 745 } 746 747 void DebugInfoBinaryPatcher::addUDataPatch(uint64_t Offset, uint64_t NewValue, 748 uint32_t OldValueSize) { 749 Offset -= DWPUnitOffset; 750 std::lock_guard<std::mutex> Lock(WriterMutex); 751 DebugPatches.emplace_back( 752 new DebugPatchVariableSize(Offset, OldValueSize, NewValue)); 753 } 754 755 void DebugInfoBinaryPatcher::addLE64Patch(uint64_t Offset, uint64_t NewValue) { 756 Offset -= DWPUnitOffset; 757 std::lock_guard<std::mutex> Lock(WriterMutex); 758 DebugPatches.emplace_back(new DebugPatch64(Offset, NewValue)); 759 } 760 761 void DebugInfoBinaryPatcher::addLE32Patch(uint64_t Offset, uint32_t NewValue, 762 uint32_t OldValueSize) { 763 Offset -= DWPUnitOffset; 764 std::lock_guard<std::mutex> Lock(WriterMutex); 765 if (OldValueSize == 4) 766 DebugPatches.emplace_back(new DebugPatch32(Offset, NewValue)); 767 else if (OldValueSize == 8) 768 DebugPatches.emplace_back(new DebugPatch64to32(Offset, NewValue)); 769 else 770 DebugPatches.emplace_back( 771 new DebugPatch32GenericSize(Offset, NewValue, OldValueSize)); 772 } 773 774 void SimpleBinaryPatcher::addBinaryPatch(uint64_t Offset, 775 std::string &&NewValue, 776 uint32_t OldValueSize) { 777 Patches.emplace_back(Offset, std::move(NewValue)); 778 } 779 780 void SimpleBinaryPatcher::addBytePatch(uint64_t Offset, uint8_t Value) { 781 auto Str = std::string(1, Value); 782 Patches.emplace_back(Offset, std::move(Str)); 783 } 784 785 void SimpleBinaryPatcher::addLEPatch(uint64_t Offset, uint64_t NewValue, 786 size_t ByteSize) { 787 Patches.emplace_back(Offset, encodeLE(ByteSize, NewValue)); 788 } 789 790 void SimpleBinaryPatcher::addUDataPatch(uint64_t Offset, uint64_t Value, 791 uint32_t OldValueSize) { 792 std::string Buff; 793 raw_string_ostream OS(Buff); 794 encodeULEB128(Value, OS, OldValueSize); 795 796 Patches.emplace_back(Offset, std::move(Buff)); 797 } 798 799 void SimpleBinaryPatcher::addLE64Patch(uint64_t Offset, uint64_t NewValue) { 800 addLEPatch(Offset, NewValue, 8); 801 } 802 803 void SimpleBinaryPatcher::addLE32Patch(uint64_t Offset, uint32_t NewValue, 804 uint32_t OldValueSize) { 805 addLEPatch(Offset, NewValue, 4); 806 } 807 808 std::string SimpleBinaryPatcher::patchBinary(StringRef BinaryContents) { 809 std::string BinaryContentsStr = std::string(BinaryContents); 810 for (const auto &Patch : Patches) { 811 uint32_t Offset = Patch.first; 812 const std::string &ByteSequence = Patch.second; 813 assert(Offset + ByteSequence.size() <= BinaryContents.size() && 814 "Applied patch runs over binary size."); 815 for (uint64_t I = 0, Size = ByteSequence.size(); I < Size; ++I) { 816 BinaryContentsStr[Offset + I] = ByteSequence[I]; 817 } 818 } 819 return BinaryContentsStr; 820 } 821 822 CUOffsetMap DebugInfoBinaryPatcher::computeNewOffsets(DWARFContext &DWCtx, 823 bool IsDWOContext) { 824 CUOffsetMap CUMap; 825 std::sort(DebugPatches.begin(), DebugPatches.end(), 826 [](const UniquePatchPtrType &V1, const UniquePatchPtrType &V2) { 827 if (V1.get()->Offset == V2.get()->Offset) { 828 if (V1->Kind == DebugPatchKind::NewDebugEntry && 829 V2->Kind == DebugPatchKind::NewDebugEntry) 830 return reinterpret_cast<const NewDebugEntry *>(V1.get()) 831 ->CurrentOrder < 832 reinterpret_cast<const NewDebugEntry *>(V2.get()) 833 ->CurrentOrder; 834 835 // This is a case where we are modifying first entry of next 836 // DIE, and adding a new one. 837 return V1->Kind == DebugPatchKind::NewDebugEntry; 838 } 839 return V1.get()->Offset < V2.get()->Offset; 840 }); 841 842 DWARFUnitVector::compile_unit_range CompileUnits = 843 IsDWOContext ? DWCtx.dwo_compile_units() : DWCtx.compile_units(); 844 845 for (const std::unique_ptr<DWARFUnit> &CU : CompileUnits) 846 CUMap[CU->getOffset()] = {static_cast<uint32_t>(CU->getOffset()), 847 static_cast<uint32_t>(CU->getLength())}; 848 849 // Calculating changes in .debug_info size from Patches to build a map of old 850 // to updated reference destination offsets. 851 uint32_t PreviousOffset = 0; 852 int32_t PreviousChangeInSize = 0; 853 for (UniquePatchPtrType &PatchBase : DebugPatches) { 854 Patch *P = PatchBase.get(); 855 switch (P->Kind) { 856 default: 857 continue; 858 case DebugPatchKind::PatchValue64to32: { 859 PreviousChangeInSize -= 4; 860 break; 861 } 862 case DebugPatchKind::PatchValue32GenericSize: { 863 DebugPatch32GenericSize *DPVS = 864 reinterpret_cast<DebugPatch32GenericSize *>(P); 865 PreviousChangeInSize += 4 - DPVS->OldValueSize; 866 break; 867 } 868 case DebugPatchKind::PatchValueVariable: { 869 DebugPatchVariableSize *DPV = 870 reinterpret_cast<DebugPatchVariableSize *>(P); 871 std::string Temp; 872 raw_string_ostream OS(Temp); 873 encodeULEB128(DPV->Value, OS); 874 PreviousChangeInSize += Temp.size() - DPV->OldValueSize; 875 break; 876 } 877 case DebugPatchKind::DestinationReferenceLabel: { 878 DestinationReferenceLabel *DRL = 879 reinterpret_cast<DestinationReferenceLabel *>(P); 880 OldToNewOffset[DRL->Offset] = 881 DRL->Offset + ChangeInSize + PreviousChangeInSize; 882 break; 883 } 884 case DebugPatchKind::ReferencePatchValue: { 885 // This doesn't look to be a common case, so will always encode as 4 bytes 886 // to reduce algorithmic complexity. 887 DebugPatchReference *RDP = reinterpret_cast<DebugPatchReference *>(P); 888 if (RDP->PatchInfo.IndirectRelative) { 889 PreviousChangeInSize += 4 - RDP->PatchInfo.OldValueSize; 890 assert(RDP->PatchInfo.OldValueSize <= 4 && 891 "Variable encoding reference greater than 4 bytes."); 892 } 893 break; 894 } 895 case DebugPatchKind::DWARFUnitOffsetBaseLabel: { 896 DWARFUnitOffsetBaseLabel *BaseLabel = 897 reinterpret_cast<DWARFUnitOffsetBaseLabel *>(P); 898 uint32_t CUOffset = BaseLabel->Offset; 899 ChangeInSize += PreviousChangeInSize; 900 uint32_t CUOffsetUpdate = CUOffset + ChangeInSize; 901 CUMap[CUOffset].Offset = CUOffsetUpdate; 902 CUMap[PreviousOffset].Length += PreviousChangeInSize; 903 PreviousChangeInSize = 0; 904 PreviousOffset = CUOffset; 905 break; 906 } 907 case DebugPatchKind::NewDebugEntry: { 908 NewDebugEntry *NDE = reinterpret_cast<NewDebugEntry *>(P); 909 PreviousChangeInSize += NDE->Value.size(); 910 break; 911 } 912 } 913 } 914 CUMap[PreviousOffset].Length += PreviousChangeInSize; 915 return CUMap; 916 } 917 uint32_t DebugInfoBinaryPatcher::NewDebugEntry::OrderCounter = 0; 918 919 std::string DebugInfoBinaryPatcher::patchBinary(StringRef BinaryContents) { 920 std::string NewBinaryContents; 921 NewBinaryContents.reserve(BinaryContents.size() + ChangeInSize); 922 uint32_t StartOffset = 0; 923 uint32_t DwarfUnitBaseOffset = 0; 924 uint32_t OldValueSize = 0; 925 uint32_t Offset = 0; 926 std::string ByteSequence; 927 std::vector<std::pair<uint32_t, uint32_t>> LengthPatches; 928 // Wasting one entry to avoid checks for first. 929 LengthPatches.push_back({0, 0}); 930 931 // Applying all the patches replacing current entry. 932 // This might change the size of .debug_info section. 933 for (const UniquePatchPtrType &PatchBase : DebugPatches) { 934 Patch *P = PatchBase.get(); 935 switch (P->Kind) { 936 default: 937 continue; 938 case DebugPatchKind::ReferencePatchValue: { 939 DebugPatchReference *RDP = reinterpret_cast<DebugPatchReference *>(P); 940 uint32_t DestinationOffset = RDP->DestinationOffset; 941 assert(OldToNewOffset.count(DestinationOffset) && 942 "Destination Offset for reference not updated."); 943 uint32_t UpdatedOffset = OldToNewOffset[DestinationOffset]; 944 Offset = RDP->Offset; 945 OldValueSize = RDP->PatchInfo.OldValueSize; 946 if (RDP->PatchInfo.DirectRelative) { 947 UpdatedOffset -= DwarfUnitBaseOffset; 948 ByteSequence = encodeLE(OldValueSize, UpdatedOffset); 949 // In theory reference for DW_FORM_ref{1,2,4,8} can be right on the edge 950 // and overflow if later debug information grows. 951 if (ByteSequence.size() > OldValueSize) 952 errs() << "BOLT-ERROR: Relative reference of size " 953 << Twine::utohexstr(OldValueSize) 954 << " overflows with the new encoding.\n"; 955 } else if (RDP->PatchInfo.DirectAbsolute) { 956 ByteSequence = encodeLE(OldValueSize, UpdatedOffset); 957 } else if (RDP->PatchInfo.IndirectRelative) { 958 UpdatedOffset -= DwarfUnitBaseOffset; 959 ByteSequence.clear(); 960 raw_string_ostream OS(ByteSequence); 961 encodeULEB128(UpdatedOffset, OS, 4); 962 } else { 963 llvm_unreachable("Invalid Reference form."); 964 } 965 break; 966 } 967 case DebugPatchKind::PatchValue32: { 968 DebugPatch32 *P32 = reinterpret_cast<DebugPatch32 *>(P); 969 Offset = P32->Offset; 970 OldValueSize = 4; 971 ByteSequence = encodeLE(4, P32->Value); 972 break; 973 } 974 case DebugPatchKind::PatchValue64to32: { 975 DebugPatch64to32 *P64to32 = reinterpret_cast<DebugPatch64to32 *>(P); 976 Offset = P64to32->Offset; 977 OldValueSize = 8; 978 ByteSequence = encodeLE(4, P64to32->Value); 979 break; 980 } 981 case DebugPatchKind::PatchValue32GenericSize: { 982 DebugPatch32GenericSize *DPVS = 983 reinterpret_cast<DebugPatch32GenericSize *>(P); 984 Offset = DPVS->Offset; 985 OldValueSize = DPVS->OldValueSize; 986 ByteSequence = encodeLE(4, DPVS->Value); 987 break; 988 } 989 case DebugPatchKind::PatchValueVariable: { 990 DebugPatchVariableSize *PV = 991 reinterpret_cast<DebugPatchVariableSize *>(P); 992 Offset = PV->Offset; 993 OldValueSize = PV->OldValueSize; 994 ByteSequence.clear(); 995 raw_string_ostream OS(ByteSequence); 996 encodeULEB128(PV->Value, OS); 997 break; 998 } 999 case DebugPatchKind::PatchValue64: { 1000 DebugPatch64 *P64 = reinterpret_cast<DebugPatch64 *>(P); 1001 Offset = P64->Offset; 1002 OldValueSize = 8; 1003 ByteSequence = encodeLE(8, P64->Value); 1004 break; 1005 } 1006 case DebugPatchKind::DWARFUnitOffsetBaseLabel: { 1007 DWARFUnitOffsetBaseLabel *BaseLabel = 1008 reinterpret_cast<DWARFUnitOffsetBaseLabel *>(P); 1009 Offset = BaseLabel->Offset; 1010 OldValueSize = 0; 1011 ByteSequence.clear(); 1012 auto &Patch = LengthPatches.back(); 1013 // Length to copy between last patch entry and next compile unit. 1014 uint32_t RemainingLength = Offset - StartOffset; 1015 uint32_t NewCUOffset = NewBinaryContents.size() + RemainingLength; 1016 DwarfUnitBaseOffset = NewCUOffset; 1017 // Length of previous CU = This CU Offset - sizeof(length) - last CU 1018 // Offset. 1019 Patch.second = NewCUOffset - 4 - Patch.first; 1020 LengthPatches.push_back({NewCUOffset, 0}); 1021 break; 1022 } 1023 case DebugPatchKind::NewDebugEntry: { 1024 NewDebugEntry *NDE = reinterpret_cast<NewDebugEntry *>(P); 1025 Offset = NDE->Offset; 1026 OldValueSize = 0; 1027 ByteSequence = NDE->Value; 1028 break; 1029 } 1030 } 1031 1032 assert((P->Kind == DebugPatchKind::NewDebugEntry || 1033 Offset + ByteSequence.size() <= BinaryContents.size()) && 1034 "Applied patch runs over binary size."); 1035 uint32_t Length = Offset - StartOffset; 1036 NewBinaryContents.append(BinaryContents.substr(StartOffset, Length).data(), 1037 Length); 1038 NewBinaryContents.append(ByteSequence.data(), ByteSequence.size()); 1039 StartOffset = Offset + OldValueSize; 1040 } 1041 uint32_t Length = BinaryContents.size() - StartOffset; 1042 NewBinaryContents.append(BinaryContents.substr(StartOffset, Length).data(), 1043 Length); 1044 DebugPatches.clear(); 1045 1046 // Patching lengths of CUs 1047 auto &Patch = LengthPatches.back(); 1048 Patch.second = NewBinaryContents.size() - 4 - Patch.first; 1049 for (uint32_t J = 1, Size = LengthPatches.size(); J < Size; ++J) { 1050 const auto &Patch = LengthPatches[J]; 1051 ByteSequence = encodeLE(4, Patch.second); 1052 Offset = Patch.first; 1053 for (uint64_t I = 0, Size = ByteSequence.size(); I < Size; ++I) 1054 NewBinaryContents[Offset + I] = ByteSequence[I]; 1055 } 1056 1057 return NewBinaryContents; 1058 } 1059 1060 void DebugStrOffsetsWriter::initialize( 1061 const DWARFSection &StrOffsetsSection, 1062 const Optional<StrOffsetsContributionDescriptor> Contr) { 1063 if (!Contr) 1064 return; 1065 1066 const uint8_t DwarfOffsetByteSize = Contr->getDwarfOffsetByteSize(); 1067 assert(DwarfOffsetByteSize == 4 && 1068 "Dwarf String Offsets Byte Size is not supported."); 1069 uint32_t Index = 0; 1070 for (uint64_t Offset = 0; Offset < Contr->Size; Offset += DwarfOffsetByteSize) 1071 IndexToAddressMap[Index++] = *reinterpret_cast<const uint32_t *>( 1072 StrOffsetsSection.Data.data() + Contr->Base + Offset); 1073 } 1074 1075 void DebugStrOffsetsWriter::updateAddressMap(uint32_t Index, uint32_t Address) { 1076 assert(IndexToAddressMap.count(Index) > 0 && "Index is not found."); 1077 IndexToAddressMap[Index] = Address; 1078 } 1079 1080 void DebugStrOffsetsWriter::finalizeSection() { 1081 if (IndexToAddressMap.empty()) 1082 return; 1083 // Writing out the header for each section. 1084 support::endian::write(*StrOffsetsStream, CurrentSectionSize + 4, 1085 support::little); 1086 support::endian::write(*StrOffsetsStream, static_cast<uint16_t>(5), 1087 support::little); 1088 support::endian::write(*StrOffsetsStream, static_cast<uint16_t>(0), 1089 support::little); 1090 for (const auto &Entry : IndexToAddressMap) 1091 support::endian::write(*StrOffsetsStream, Entry.second, support::little); 1092 IndexToAddressMap.clear(); 1093 } 1094 1095 void DebugStrWriter::create() { 1096 StrBuffer = std::make_unique<DebugStrBufferVector>(); 1097 StrStream = std::make_unique<raw_svector_ostream>(*StrBuffer); 1098 } 1099 1100 void DebugStrWriter::initialize() { 1101 auto StrSection = BC.DwCtx->getDWARFObj().getStrSection(); 1102 (*StrStream) << StrSection; 1103 } 1104 1105 uint32_t DebugStrWriter::addString(StringRef Str) { 1106 std::lock_guard<std::mutex> Lock(WriterMutex); 1107 if (StrBuffer->empty()) 1108 initialize(); 1109 auto Offset = StrBuffer->size(); 1110 (*StrStream) << Str; 1111 StrStream->write_zeros(1); 1112 return Offset; 1113 } 1114 1115 void DebugAbbrevWriter::addUnitAbbreviations(DWARFUnit &Unit) { 1116 const DWARFAbbreviationDeclarationSet *Abbrevs = Unit.getAbbreviations(); 1117 if (!Abbrevs) 1118 return; 1119 1120 const PatchesTy &UnitPatches = Patches[&Unit]; 1121 const AbbrevEntryTy &AbbrevEntries = NewAbbrevEntries[&Unit]; 1122 1123 // We are duplicating abbrev sections, to handle the case where for one CU we 1124 // modify it, but for another we don't. 1125 auto UnitDataPtr = std::make_unique<AbbrevData>(); 1126 AbbrevData &UnitData = *UnitDataPtr.get(); 1127 UnitData.Buffer = std::make_unique<DebugBufferVector>(); 1128 UnitData.Stream = std::make_unique<raw_svector_ostream>(*UnitData.Buffer); 1129 1130 raw_svector_ostream &OS = *UnitData.Stream.get(); 1131 1132 // Returns true if AbbrevData is re-used, false otherwise. 1133 auto hashAndAddAbbrev = [&](StringRef AbbrevData) -> bool { 1134 llvm::SHA1 Hasher; 1135 Hasher.update(AbbrevData); 1136 std::array<uint8_t, 20> Hash = Hasher.final(); 1137 StringRef Key((const char *)Hash.data(), Hash.size()); 1138 auto Iter = AbbrevDataCache.find(Key); 1139 if (Iter != AbbrevDataCache.end()) { 1140 UnitsAbbrevData[&Unit] = Iter->second.get(); 1141 return true; 1142 } 1143 AbbrevDataCache[Key] = std::move(UnitDataPtr); 1144 UnitsAbbrevData[&Unit] = &UnitData; 1145 return false; 1146 }; 1147 // Take a fast path if there are no patches to apply. Simply copy the original 1148 // contents. 1149 if (UnitPatches.empty() && AbbrevEntries.empty()) { 1150 StringRef AbbrevSectionContents = 1151 Unit.isDWOUnit() ? Unit.getContext().getDWARFObj().getAbbrevDWOSection() 1152 : Unit.getContext().getDWARFObj().getAbbrevSection(); 1153 StringRef AbbrevContents; 1154 1155 const DWARFUnitIndex &CUIndex = Unit.getContext().getCUIndex(); 1156 if (!CUIndex.getRows().empty()) { 1157 // Handle DWP section contribution. 1158 const DWARFUnitIndex::Entry *DWOEntry = 1159 CUIndex.getFromHash(*Unit.getDWOId()); 1160 if (!DWOEntry) 1161 return; 1162 1163 const DWARFUnitIndex::Entry::SectionContribution *DWOContrubution = 1164 DWOEntry->getContribution(DWARFSectionKind::DW_SECT_ABBREV); 1165 AbbrevContents = AbbrevSectionContents.substr(DWOContrubution->Offset, 1166 DWOContrubution->Length); 1167 } else if (!Unit.isDWOUnit()) { 1168 const uint64_t StartOffset = Unit.getAbbreviationsOffset(); 1169 1170 // We know where the unit's abbreviation set starts, but not where it ends 1171 // as such data is not readily available. Hence, we have to build a sorted 1172 // list of start addresses and find the next starting address to determine 1173 // the set boundaries. 1174 // 1175 // FIXME: if we had a full access to DWARFDebugAbbrev::AbbrDeclSets 1176 // we wouldn't have to build our own sorted list for the quick lookup. 1177 if (AbbrevSetOffsets.empty()) { 1178 for_each( 1179 *Unit.getContext().getDebugAbbrev(), 1180 [&](const std::pair<uint64_t, DWARFAbbreviationDeclarationSet> &P) { 1181 AbbrevSetOffsets.push_back(P.first); 1182 }); 1183 sort(AbbrevSetOffsets); 1184 } 1185 auto It = upper_bound(AbbrevSetOffsets, StartOffset); 1186 const uint64_t EndOffset = 1187 It == AbbrevSetOffsets.end() ? AbbrevSectionContents.size() : *It; 1188 AbbrevContents = AbbrevSectionContents.slice(StartOffset, EndOffset); 1189 } else { 1190 // For DWO unit outside of DWP, we expect the entire section to hold 1191 // abbreviations for this unit only. 1192 AbbrevContents = AbbrevSectionContents; 1193 } 1194 1195 if (!hashAndAddAbbrev(AbbrevContents)) { 1196 OS.reserveExtraSpace(AbbrevContents.size()); 1197 OS << AbbrevContents; 1198 } 1199 return; 1200 } 1201 1202 for (auto I = Abbrevs->begin(), E = Abbrevs->end(); I != E; ++I) { 1203 const DWARFAbbreviationDeclaration &Abbrev = *I; 1204 auto Patch = UnitPatches.find(&Abbrev); 1205 1206 encodeULEB128(Abbrev.getCode(), OS); 1207 encodeULEB128(Abbrev.getTag(), OS); 1208 encodeULEB128(Abbrev.hasChildren(), OS); 1209 for (const DWARFAbbreviationDeclaration::AttributeSpec &AttrSpec : 1210 Abbrev.attributes()) { 1211 if (Patch != UnitPatches.end()) { 1212 bool Patched = false; 1213 // Patches added later take a precedence over earlier ones. 1214 for (auto I = Patch->second.rbegin(), E = Patch->second.rend(); I != E; 1215 ++I) { 1216 if (I->OldAttr != AttrSpec.Attr) 1217 continue; 1218 1219 encodeULEB128(I->NewAttr, OS); 1220 encodeULEB128(I->NewAttrForm, OS); 1221 Patched = true; 1222 break; 1223 } 1224 if (Patched) 1225 continue; 1226 } 1227 1228 encodeULEB128(AttrSpec.Attr, OS); 1229 encodeULEB128(AttrSpec.Form, OS); 1230 if (AttrSpec.isImplicitConst()) 1231 encodeSLEB128(AttrSpec.getImplicitConstValue(), OS); 1232 } 1233 const auto Entries = AbbrevEntries.find(&Abbrev); 1234 // Adding new Abbrevs for inserted entries. 1235 if (Entries != AbbrevEntries.end()) { 1236 for (const AbbrevEntry &Entry : Entries->second) { 1237 encodeULEB128(Entry.Attr, OS); 1238 encodeULEB128(Entry.Form, OS); 1239 } 1240 } 1241 encodeULEB128(0, OS); 1242 encodeULEB128(0, OS); 1243 } 1244 encodeULEB128(0, OS); 1245 1246 hashAndAddAbbrev(OS.str()); 1247 } 1248 1249 std::unique_ptr<DebugBufferVector> DebugAbbrevWriter::finalize() { 1250 // Used to create determinism for writing out abbrevs. 1251 std::vector<AbbrevData *> Abbrevs; 1252 if (DWOId) { 1253 // We expect abbrev_offset to always be zero for DWO units as there 1254 // should be one CU per DWO, and TUs should share the same abbreviation 1255 // set with the CU. 1256 // For DWP AbbreviationsOffset is an Abbrev contribution in the DWP file, so 1257 // can be none zero. Thus we are skipping the check for DWP. 1258 bool IsDWP = !Context.getCUIndex().getRows().empty(); 1259 if (!IsDWP) { 1260 for (const std::unique_ptr<DWARFUnit> &Unit : Context.dwo_units()) { 1261 if (Unit->getAbbreviationsOffset() != 0) { 1262 errs() << "BOLT-ERROR: detected DWO unit with non-zero abbr_offset. " 1263 "Unable to update debug info.\n"; 1264 exit(1); 1265 } 1266 } 1267 } 1268 1269 DWARFUnit *Unit = Context.getDWOCompileUnitForHash(*DWOId); 1270 // Issue abbreviations for the DWO CU only. 1271 addUnitAbbreviations(*Unit); 1272 AbbrevData *Abbrev = UnitsAbbrevData[Unit]; 1273 Abbrevs.push_back(Abbrev); 1274 } else { 1275 Abbrevs.reserve(Context.getNumCompileUnits() + Context.getNumTypeUnits()); 1276 std::unordered_set<AbbrevData *> ProcessedAbbrevs; 1277 // Add abbreviations from compile and type non-DWO units. 1278 for (const std::unique_ptr<DWARFUnit> &Unit : Context.normal_units()) { 1279 addUnitAbbreviations(*Unit); 1280 AbbrevData *Abbrev = UnitsAbbrevData[Unit.get()]; 1281 if (!ProcessedAbbrevs.insert(Abbrev).second) 1282 continue; 1283 Abbrevs.push_back(Abbrev); 1284 } 1285 } 1286 1287 DebugBufferVector ReturnBuffer; 1288 // Pre-calculate the total size of abbrev section. 1289 uint64_t Size = 0; 1290 for (const AbbrevData *UnitData : Abbrevs) 1291 Size += UnitData->Buffer->size(); 1292 1293 ReturnBuffer.reserve(Size); 1294 1295 uint64_t Pos = 0; 1296 for (AbbrevData *UnitData : Abbrevs) { 1297 ReturnBuffer.append(*UnitData->Buffer); 1298 UnitData->Offset = Pos; 1299 Pos += UnitData->Buffer->size(); 1300 1301 UnitData->Buffer.reset(); 1302 UnitData->Stream.reset(); 1303 } 1304 1305 return std::make_unique<DebugBufferVector>(ReturnBuffer); 1306 } 1307 1308 static void emitDwarfSetLineAddrAbs(MCStreamer &OS, 1309 MCDwarfLineTableParams Params, 1310 int64_t LineDelta, uint64_t Address, 1311 int PointerSize) { 1312 // emit the sequence to set the address 1313 OS.emitIntValue(dwarf::DW_LNS_extended_op, 1); 1314 OS.emitULEB128IntValue(PointerSize + 1); 1315 OS.emitIntValue(dwarf::DW_LNE_set_address, 1); 1316 OS.emitIntValue(Address, PointerSize); 1317 1318 // emit the sequence for the LineDelta (from 1) and a zero address delta. 1319 MCDwarfLineAddr::Emit(&OS, Params, LineDelta, 0); 1320 } 1321 1322 static inline void emitBinaryDwarfLineTable( 1323 MCStreamer *MCOS, MCDwarfLineTableParams Params, 1324 const DWARFDebugLine::LineTable *Table, 1325 const std::vector<DwarfLineTable::RowSequence> &InputSequences) { 1326 if (InputSequences.empty()) 1327 return; 1328 1329 constexpr uint64_t InvalidAddress = UINT64_MAX; 1330 unsigned FileNum = 1; 1331 unsigned LastLine = 1; 1332 unsigned Column = 0; 1333 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 1334 unsigned Isa = 0; 1335 unsigned Discriminator = 0; 1336 uint64_t LastAddress = InvalidAddress; 1337 uint64_t PrevEndOfSequence = InvalidAddress; 1338 const MCAsmInfo *AsmInfo = MCOS->getContext().getAsmInfo(); 1339 1340 auto emitEndOfSequence = [&](uint64_t Address) { 1341 MCDwarfLineAddr::Emit(MCOS, Params, INT64_MAX, Address - LastAddress); 1342 FileNum = 1; 1343 LastLine = 1; 1344 Column = 0; 1345 Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 1346 Isa = 0; 1347 Discriminator = 0; 1348 LastAddress = InvalidAddress; 1349 }; 1350 1351 for (const DwarfLineTable::RowSequence &Sequence : InputSequences) { 1352 const uint64_t SequenceStart = 1353 Table->Rows[Sequence.FirstIndex].Address.Address; 1354 1355 // Check if we need to mark the end of the sequence. 1356 if (PrevEndOfSequence != InvalidAddress && LastAddress != InvalidAddress && 1357 PrevEndOfSequence != SequenceStart) { 1358 emitEndOfSequence(PrevEndOfSequence); 1359 } 1360 1361 for (uint32_t RowIndex = Sequence.FirstIndex; 1362 RowIndex <= Sequence.LastIndex; ++RowIndex) { 1363 const DWARFDebugLine::Row &Row = Table->Rows[RowIndex]; 1364 int64_t LineDelta = static_cast<int64_t>(Row.Line) - LastLine; 1365 const uint64_t Address = Row.Address.Address; 1366 1367 if (FileNum != Row.File) { 1368 FileNum = Row.File; 1369 MCOS->emitInt8(dwarf::DW_LNS_set_file); 1370 MCOS->emitULEB128IntValue(FileNum); 1371 } 1372 if (Column != Row.Column) { 1373 Column = Row.Column; 1374 MCOS->emitInt8(dwarf::DW_LNS_set_column); 1375 MCOS->emitULEB128IntValue(Column); 1376 } 1377 if (Discriminator != Row.Discriminator && 1378 MCOS->getContext().getDwarfVersion() >= 4) { 1379 Discriminator = Row.Discriminator; 1380 unsigned Size = getULEB128Size(Discriminator); 1381 MCOS->emitInt8(dwarf::DW_LNS_extended_op); 1382 MCOS->emitULEB128IntValue(Size + 1); 1383 MCOS->emitInt8(dwarf::DW_LNE_set_discriminator); 1384 MCOS->emitULEB128IntValue(Discriminator); 1385 } 1386 if (Isa != Row.Isa) { 1387 Isa = Row.Isa; 1388 MCOS->emitInt8(dwarf::DW_LNS_set_isa); 1389 MCOS->emitULEB128IntValue(Isa); 1390 } 1391 if (Row.IsStmt != Flags) { 1392 Flags = Row.IsStmt; 1393 MCOS->emitInt8(dwarf::DW_LNS_negate_stmt); 1394 } 1395 if (Row.BasicBlock) 1396 MCOS->emitInt8(dwarf::DW_LNS_set_basic_block); 1397 if (Row.PrologueEnd) 1398 MCOS->emitInt8(dwarf::DW_LNS_set_prologue_end); 1399 if (Row.EpilogueBegin) 1400 MCOS->emitInt8(dwarf::DW_LNS_set_epilogue_begin); 1401 1402 // The end of the sequence is not normal in the middle of the input 1403 // sequence, but could happen, e.g. for assembly code. 1404 if (Row.EndSequence) { 1405 emitEndOfSequence(Address); 1406 } else { 1407 if (LastAddress == InvalidAddress) 1408 emitDwarfSetLineAddrAbs(*MCOS, Params, LineDelta, Address, 1409 AsmInfo->getCodePointerSize()); 1410 else 1411 MCDwarfLineAddr::Emit(MCOS, Params, LineDelta, Address - LastAddress); 1412 1413 LastAddress = Address; 1414 LastLine = Row.Line; 1415 } 1416 1417 Discriminator = 0; 1418 } 1419 PrevEndOfSequence = Sequence.EndAddress; 1420 } 1421 1422 // Finish with the end of the sequence. 1423 if (LastAddress != InvalidAddress) 1424 emitEndOfSequence(PrevEndOfSequence); 1425 } 1426 1427 // This function is similar to the one from MCDwarfLineTable, except it handles 1428 // end-of-sequence entries differently by utilizing line entries with 1429 // DWARF2_FLAG_END_SEQUENCE flag. 1430 static inline void emitDwarfLineTable( 1431 MCStreamer *MCOS, MCSection *Section, 1432 const MCLineSection::MCDwarfLineEntryCollection &LineEntries) { 1433 unsigned FileNum = 1; 1434 unsigned LastLine = 1; 1435 unsigned Column = 0; 1436 unsigned Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 1437 unsigned Isa = 0; 1438 unsigned Discriminator = 0; 1439 MCSymbol *LastLabel = nullptr; 1440 const MCAsmInfo *AsmInfo = MCOS->getContext().getAsmInfo(); 1441 1442 // Loop through each MCDwarfLineEntry and encode the dwarf line number table. 1443 for (const MCDwarfLineEntry &LineEntry : LineEntries) { 1444 if (LineEntry.getFlags() & DWARF2_FLAG_END_SEQUENCE) { 1445 MCOS->emitDwarfAdvanceLineAddr(INT64_MAX, LastLabel, LineEntry.getLabel(), 1446 AsmInfo->getCodePointerSize()); 1447 FileNum = 1; 1448 LastLine = 1; 1449 Column = 0; 1450 Flags = DWARF2_LINE_DEFAULT_IS_STMT ? DWARF2_FLAG_IS_STMT : 0; 1451 Isa = 0; 1452 Discriminator = 0; 1453 LastLabel = nullptr; 1454 continue; 1455 } 1456 1457 int64_t LineDelta = static_cast<int64_t>(LineEntry.getLine()) - LastLine; 1458 1459 if (FileNum != LineEntry.getFileNum()) { 1460 FileNum = LineEntry.getFileNum(); 1461 MCOS->emitInt8(dwarf::DW_LNS_set_file); 1462 MCOS->emitULEB128IntValue(FileNum); 1463 } 1464 if (Column != LineEntry.getColumn()) { 1465 Column = LineEntry.getColumn(); 1466 MCOS->emitInt8(dwarf::DW_LNS_set_column); 1467 MCOS->emitULEB128IntValue(Column); 1468 } 1469 if (Discriminator != LineEntry.getDiscriminator() && 1470 MCOS->getContext().getDwarfVersion() >= 2) { 1471 Discriminator = LineEntry.getDiscriminator(); 1472 unsigned Size = getULEB128Size(Discriminator); 1473 MCOS->emitInt8(dwarf::DW_LNS_extended_op); 1474 MCOS->emitULEB128IntValue(Size + 1); 1475 MCOS->emitInt8(dwarf::DW_LNE_set_discriminator); 1476 MCOS->emitULEB128IntValue(Discriminator); 1477 } 1478 if (Isa != LineEntry.getIsa()) { 1479 Isa = LineEntry.getIsa(); 1480 MCOS->emitInt8(dwarf::DW_LNS_set_isa); 1481 MCOS->emitULEB128IntValue(Isa); 1482 } 1483 if ((LineEntry.getFlags() ^ Flags) & DWARF2_FLAG_IS_STMT) { 1484 Flags = LineEntry.getFlags(); 1485 MCOS->emitInt8(dwarf::DW_LNS_negate_stmt); 1486 } 1487 if (LineEntry.getFlags() & DWARF2_FLAG_BASIC_BLOCK) 1488 MCOS->emitInt8(dwarf::DW_LNS_set_basic_block); 1489 if (LineEntry.getFlags() & DWARF2_FLAG_PROLOGUE_END) 1490 MCOS->emitInt8(dwarf::DW_LNS_set_prologue_end); 1491 if (LineEntry.getFlags() & DWARF2_FLAG_EPILOGUE_BEGIN) 1492 MCOS->emitInt8(dwarf::DW_LNS_set_epilogue_begin); 1493 1494 MCSymbol *Label = LineEntry.getLabel(); 1495 1496 // At this point we want to emit/create the sequence to encode the delta 1497 // in line numbers and the increment of the address from the previous 1498 // Label and the current Label. 1499 MCOS->emitDwarfAdvanceLineAddr(LineDelta, LastLabel, Label, 1500 AsmInfo->getCodePointerSize()); 1501 Discriminator = 0; 1502 LastLine = LineEntry.getLine(); 1503 LastLabel = Label; 1504 } 1505 1506 assert(LastLabel == nullptr && "end of sequence expected"); 1507 } 1508 1509 void DwarfLineTable::emitCU(MCStreamer *MCOS, MCDwarfLineTableParams Params, 1510 Optional<MCDwarfLineStr> &LineStr, 1511 BinaryContext &BC) const { 1512 if (!RawData.empty()) { 1513 assert(MCLineSections.getMCLineEntries().empty() && 1514 InputSequences.empty() && 1515 "cannot combine raw data with new line entries"); 1516 MCOS->emitLabel(getLabel()); 1517 MCOS->emitBytes(RawData); 1518 1519 // Emit fake relocation for RuntimeDyld to always allocate the section. 1520 // 1521 // FIXME: remove this once RuntimeDyld stops skipping allocatable sections 1522 // without relocations. 1523 MCOS->emitRelocDirective( 1524 *MCConstantExpr::create(0, *BC.Ctx), "BFD_RELOC_NONE", 1525 MCSymbolRefExpr::create(getLabel(), *BC.Ctx), SMLoc(), *BC.STI); 1526 1527 return; 1528 } 1529 1530 MCSymbol *LineEndSym = Header.Emit(MCOS, Params, LineStr).second; 1531 1532 // Put out the line tables. 1533 for (const auto &LineSec : MCLineSections.getMCLineEntries()) 1534 emitDwarfLineTable(MCOS, LineSec.first, LineSec.second); 1535 1536 // Emit line tables for the original code. 1537 emitBinaryDwarfLineTable(MCOS, Params, InputTable, InputSequences); 1538 1539 // This is the end of the section, so set the value of the symbol at the end 1540 // of this section (that was used in a previous expression). 1541 MCOS->emitLabel(LineEndSym); 1542 } 1543 1544 // Helper function to parse .debug_line_str, and populate one we are using. 1545 // For functions that we do not modify we output them as raw data. 1546 // Re-constructing .debug_line_str so that offsets are correct for those 1547 // debut line tables. 1548 // Bonus is that when we output a final binary we can re-use .debug_line_str 1549 // section. So we don't have to do the SHF_ALLOC trick we did with 1550 // .debug_line. 1551 static void parseAndPopulateDebugLineStr(BinarySection &LineStrSection, 1552 MCDwarfLineStr &LineStr, 1553 BinaryContext &BC, 1554 MCStreamer &Streamer) { 1555 DataExtractor StrData(LineStrSection.getContents(), 1556 BC.DwCtx->isLittleEndian(), 0); 1557 uint64_t Offset = 0; 1558 while (StrData.isValidOffset(Offset)) { 1559 Error Err = Error::success(); 1560 const char *CStr = StrData.getCStr(&Offset, &Err); 1561 if (Err) { 1562 errs() << "BOLT-ERROR: could not extract string from .debug_line_str"; 1563 continue; 1564 } 1565 LineStr.emitRef(&Streamer, CStr); 1566 } 1567 } 1568 1569 void DwarfLineTable::emit(BinaryContext &BC, MCStreamer &Streamer) { 1570 MCAssembler &Assembler = 1571 static_cast<MCObjectStreamer *>(&Streamer)->getAssembler(); 1572 1573 MCDwarfLineTableParams Params = Assembler.getDWARFLinetableParams(); 1574 1575 auto &LineTables = BC.getDwarfLineTables(); 1576 1577 // Bail out early so we don't switch to the debug_line section needlessly and 1578 // in doing so create an unnecessary (if empty) section. 1579 if (LineTables.empty()) 1580 return; 1581 // In a v5 non-split line table, put the strings in a separate section. 1582 Optional<MCDwarfLineStr> LineStr(None); 1583 ErrorOr<BinarySection &> LineStrSection = 1584 BC.getUniqueSectionByName(".debug_line_str"); 1585 // Some versions of GCC output DWARF5 .debug_info, but DWARF4 or lower 1586 // .debug_line 1587 if (LineStrSection) { 1588 LineStr = MCDwarfLineStr(*BC.Ctx); 1589 parseAndPopulateDebugLineStr(*LineStrSection, *LineStr, BC, Streamer); 1590 } 1591 1592 // Switch to the section where the table will be emitted into. 1593 Streamer.SwitchSection(BC.MOFI->getDwarfLineSection()); 1594 1595 const uint16_t DwarfVersion = BC.Ctx->getDwarfVersion(); 1596 // Handle the rest of the Compile Units. 1597 for (auto &CUIDTablePair : LineTables) { 1598 Streamer.getContext().setDwarfVersion( 1599 CUIDTablePair.second.getDwarfVersion()); 1600 CUIDTablePair.second.emitCU(&Streamer, Params, LineStr, BC); 1601 } 1602 1603 // Resetting DWARF version for rest of the flow. 1604 BC.Ctx->setDwarfVersion(DwarfVersion); 1605 1606 // Still need to write the section out for the ExecutionEngine, and temp in 1607 // memory object we are constructing. 1608 if (LineStr) { 1609 LineStr->emitSection(&Streamer); 1610 SmallString<0> Data = LineStr->getFinalizedData(); 1611 BC.registerOrUpdateNoteSection(".debug_line_str", copyByteArray(Data.str()), 1612 Data.size()); 1613 } 1614 } 1615 1616 } // namespace bolt 1617 } // namespace llvm 1618