| 1 | //===- DwarfStreamer.cpp --------------------------------------------------===// |
| 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 | #include "llvm/DWARFLinker/Classic/DWARFStreamer.h" |
| 10 | #include "llvm/CodeGen/NonRelocatableStringpool.h" |
| 11 | #include "llvm/DWARFLinker/Classic/DWARFLinkerCompileUnit.h" |
| 12 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 13 | #include "llvm/DebugInfo/DWARF/DWARFDebugMacro.h" |
| 14 | #include "llvm/MC/MCAsmBackend.h" |
| 15 | #include "llvm/MC/MCCodeEmitter.h" |
| 16 | #include "llvm/MC/MCDwarf.h" |
| 17 | #include "llvm/MC/MCInstPrinter.h" |
| 18 | #include "llvm/MC/MCObjectWriter.h" |
| 19 | #include "llvm/MC/MCSection.h" |
| 20 | #include "llvm/MC/MCStreamer.h" |
| 21 | #include "llvm/MC/MCTargetOptions.h" |
| 22 | #include "llvm/MC/MCTargetOptionsCommandFlags.h" |
| 23 | #include "llvm/MC/TargetRegistry.h" |
| 24 | #include "llvm/Support/FormatVariadic.h" |
| 25 | #include "llvm/Support/LEB128.h" |
| 26 | #include "llvm/Target/TargetOptions.h" |
| 27 | #include "llvm/TargetParser/Triple.h" |
| 28 | |
| 29 | using namespace llvm; |
| 30 | using namespace dwarf_linker; |
| 31 | using namespace dwarf_linker::classic; |
| 32 | |
| 33 | Expected<std::unique_ptr<DwarfStreamer>> DwarfStreamer::createStreamer( |
| 34 | const Triple &TheTriple, DWARFLinkerBase::OutputFileType FileType, |
| 35 | raw_pwrite_stream &OutFile, DWARFLinkerBase::MessageHandlerTy Warning) { |
| 36 | std::unique_ptr<DwarfStreamer> Streamer = |
| 37 | std::make_unique<DwarfStreamer>(args&: FileType, args&: OutFile, args&: Warning); |
| 38 | if (Error Err = Streamer->init(TheTriple, Swift5ReflectionSegmentName: "__DWARF" )) |
| 39 | return std::move(Err); |
| 40 | |
| 41 | return std::move(Streamer); |
| 42 | } |
| 43 | |
| 44 | Error DwarfStreamer::init(Triple TheTriple, |
| 45 | StringRef Swift5ReflectionSegmentName) { |
| 46 | std::string ErrorStr; |
| 47 | std::string TripleName; |
| 48 | |
| 49 | // Get the target. |
| 50 | const Target *TheTarget = |
| 51 | TargetRegistry::lookupTarget(ArchName: TripleName, TheTriple, Error&: ErrorStr); |
| 52 | if (!TheTarget) |
| 53 | return createStringError(EC: std::errc::invalid_argument, Fmt: ErrorStr.c_str()); |
| 54 | |
| 55 | TripleName = TheTriple.getTriple(); |
| 56 | |
| 57 | // Create all the MC Objects. |
| 58 | MRI.reset(p: TheTarget->createMCRegInfo(TT: TheTriple)); |
| 59 | if (!MRI) |
| 60 | return createStringError(EC: std::errc::invalid_argument, |
| 61 | Fmt: "no register info for target %s" , |
| 62 | Vals: TripleName.c_str()); |
| 63 | |
| 64 | MCOptions = mc::InitMCTargetOptionsFromFlags(); |
| 65 | MCOptions.AsmVerbose = true; |
| 66 | MCOptions.MCUseDwarfDirectory = MCTargetOptions::EnableDwarfDirectory; |
| 67 | MAI.reset(p: TheTarget->createMCAsmInfo(MRI: *MRI, TheTriple, Options: MCOptions)); |
| 68 | if (!MAI) |
| 69 | return createStringError(EC: std::errc::invalid_argument, |
| 70 | Fmt: "no asm info for target %s" , Vals: TripleName.c_str()); |
| 71 | |
| 72 | MSTI.reset(p: TheTarget->createMCSubtargetInfo(TheTriple, CPU: "" , Features: "" )); |
| 73 | if (!MSTI) |
| 74 | return createStringError(EC: std::errc::invalid_argument, |
| 75 | Fmt: "no subtarget info for target %s" , |
| 76 | Vals: TripleName.c_str()); |
| 77 | |
| 78 | MC.reset(p: new MCContext(TheTriple, *MAI, *MRI, *MSTI, nullptr, true, |
| 79 | Swift5ReflectionSegmentName)); |
| 80 | MOFI.reset(p: TheTarget->createMCObjectFileInfo(Ctx&: *MC, /*PIC=*/false, LargeCodeModel: false)); |
| 81 | MC->setObjectFileInfo(MOFI.get()); |
| 82 | |
| 83 | MAB = TheTarget->createMCAsmBackend(STI: *MSTI, MRI: *MRI, Options: MCOptions); |
| 84 | if (!MAB) |
| 85 | return createStringError(EC: std::errc::invalid_argument, |
| 86 | Fmt: "no asm backend for target %s" , |
| 87 | Vals: TripleName.c_str()); |
| 88 | |
| 89 | MII.reset(p: TheTarget->createMCInstrInfo()); |
| 90 | if (!MII) |
| 91 | return createStringError(EC: std::errc::invalid_argument, |
| 92 | Fmt: "no instr info info for target %s" , |
| 93 | Vals: TripleName.c_str()); |
| 94 | |
| 95 | MCE = TheTarget->createMCCodeEmitter(II: *MII, Ctx&: *MC); |
| 96 | if (!MCE) |
| 97 | return createStringError(EC: std::errc::invalid_argument, |
| 98 | Fmt: "no code emitter for target %s" , |
| 99 | Vals: TripleName.c_str()); |
| 100 | |
| 101 | switch (OutFileType) { |
| 102 | case DWARFLinker::OutputFileType::Assembly: { |
| 103 | std::unique_ptr<MCInstPrinter> MIP(TheTarget->createMCInstPrinter( |
| 104 | T: TheTriple, SyntaxVariant: MAI->getAssemblerDialect(), MAI: *MAI, MII: *MII, MRI: *MRI)); |
| 105 | MS = TheTarget->createAsmStreamer( |
| 106 | Ctx&: *MC, OS: std::make_unique<formatted_raw_ostream>(args&: OutFile), IP: std::move(MIP), |
| 107 | CE: std::unique_ptr<MCCodeEmitter>(MCE), |
| 108 | TAB: std::unique_ptr<MCAsmBackend>(MAB)); |
| 109 | break; |
| 110 | } |
| 111 | case DWARFLinker::OutputFileType::Object: { |
| 112 | MS = TheTarget->createMCObjectStreamer( |
| 113 | T: TheTriple, Ctx&: *MC, TAB: std::unique_ptr<MCAsmBackend>(MAB), |
| 114 | OW: MAB->createObjectWriter(OS&: OutFile), Emitter: std::unique_ptr<MCCodeEmitter>(MCE), |
| 115 | STI: *MSTI); |
| 116 | break; |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | if (!MS) |
| 121 | return createStringError(EC: std::errc::invalid_argument, |
| 122 | Fmt: "no object streamer for target %s" , |
| 123 | Vals: TripleName.c_str()); |
| 124 | |
| 125 | // Finally create the AsmPrinter we'll use to emit the DIEs. |
| 126 | TM.reset(p: TheTarget->createTargetMachine(TT: TheTriple, CPU: "" , Features: "" , Options: TargetOptions(), |
| 127 | RM: std::nullopt)); |
| 128 | if (!TM) |
| 129 | return createStringError(EC: std::errc::invalid_argument, |
| 130 | Fmt: "no target machine for target %s" , |
| 131 | Vals: TripleName.c_str()); |
| 132 | |
| 133 | Asm.reset(p: TheTarget->createAsmPrinter(TM&: *TM, Streamer: std::unique_ptr<MCStreamer>(MS))); |
| 134 | if (!Asm) |
| 135 | return createStringError(EC: std::errc::invalid_argument, |
| 136 | Fmt: "no asm printer for target %s" , |
| 137 | Vals: TripleName.c_str()); |
| 138 | Asm->setDwarfUsesRelocationsAcrossSections(false); |
| 139 | |
| 140 | RangesSectionSize = 0; |
| 141 | RngListsSectionSize = 0; |
| 142 | LocSectionSize = 0; |
| 143 | LocListsSectionSize = 0; |
| 144 | LineSectionSize = 0; |
| 145 | FrameSectionSize = 0; |
| 146 | DebugInfoSectionSize = 0; |
| 147 | MacInfoSectionSize = 0; |
| 148 | MacroSectionSize = 0; |
| 149 | |
| 150 | return Error::success(); |
| 151 | } |
| 152 | |
| 153 | void DwarfStreamer::finish() { MS->finish(); } |
| 154 | |
| 155 | void DwarfStreamer::switchToDebugInfoSection(unsigned DwarfVersion) { |
| 156 | MS->switchSection(Section: MOFI->getDwarfInfoSection()); |
| 157 | MC->setDwarfVersion(DwarfVersion); |
| 158 | } |
| 159 | |
| 160 | /// Emit the compilation unit header for \p Unit in the debug_info section. |
| 161 | /// |
| 162 | /// A Dwarf 4 section header is encoded as: |
| 163 | /// uint32_t Unit length (omitting this field) |
| 164 | /// uint16_t Version |
| 165 | /// uint32_t Abbreviation table offset |
| 166 | /// uint8_t Address size |
| 167 | /// Leading to a total of 11 bytes. |
| 168 | /// |
| 169 | /// A Dwarf 5 section header is encoded as: |
| 170 | /// uint32_t Unit length (omitting this field) |
| 171 | /// uint16_t Version |
| 172 | /// uint8_t Unit type |
| 173 | /// uint8_t Address size |
| 174 | /// uint32_t Abbreviation table offset |
| 175 | /// Leading to a total of 12 bytes. |
| 176 | void DwarfStreamer::(CompileUnit &Unit, |
| 177 | unsigned DwarfVersion) { |
| 178 | switchToDebugInfoSection(DwarfVersion); |
| 179 | |
| 180 | /// The start of the unit within its section. |
| 181 | Unit.setLabelBegin(Asm->createTempSymbol(Name: "cu_begin" )); |
| 182 | Asm->OutStreamer->emitLabel(Symbol: Unit.getLabelBegin()); |
| 183 | |
| 184 | // Emit size of content not including length itself. The size has already |
| 185 | // been computed in CompileUnit::computeOffsets(). Subtract 4 to that size to |
| 186 | // account for the length field. |
| 187 | Asm->emitInt32(Value: Unit.getNextUnitOffset() - Unit.getStartOffset() - 4); |
| 188 | Asm->emitInt16(Value: DwarfVersion); |
| 189 | |
| 190 | if (DwarfVersion >= 5) { |
| 191 | dwarf::Tag RootTag = Unit.getOutputUnitDIE()->getTag(); |
| 192 | Asm->emitInt8(Value: RootTag == dwarf::DW_TAG_partial_unit ? dwarf::DW_UT_partial |
| 193 | : dwarf::DW_UT_compile); |
| 194 | Asm->emitInt8(Value: Unit.getOrigUnit().getAddressByteSize()); |
| 195 | // We share one abbreviations table across all units so it's always at the |
| 196 | // start of the section. |
| 197 | Asm->emitInt32(Value: 0); |
| 198 | DebugInfoSectionSize += 12; |
| 199 | } else { |
| 200 | // We share one abbreviations table across all units so it's always at the |
| 201 | // start of the section. |
| 202 | Asm->emitInt32(Value: 0); |
| 203 | Asm->emitInt8(Value: Unit.getOrigUnit().getAddressByteSize()); |
| 204 | DebugInfoSectionSize += 11; |
| 205 | } |
| 206 | |
| 207 | // Remember this CU. |
| 208 | EmittedUnits.push_back(x: {.ID: Unit.getUniqueID(), .LabelBegin: Unit.getLabelBegin()}); |
| 209 | } |
| 210 | |
| 211 | /// Emit the \p Abbrevs array as the shared abbreviation table |
| 212 | /// for the linked Dwarf file. |
| 213 | void DwarfStreamer::emitAbbrevs( |
| 214 | const std::vector<std::unique_ptr<DIEAbbrev>> &Abbrevs, |
| 215 | unsigned DwarfVersion) { |
| 216 | MS->switchSection(Section: MOFI->getDwarfAbbrevSection()); |
| 217 | MC->setDwarfVersion(DwarfVersion); |
| 218 | Asm->emitDwarfAbbrevs(Abbrevs); |
| 219 | } |
| 220 | |
| 221 | /// Recursively emit the DIE tree rooted at \p Die. |
| 222 | void DwarfStreamer::emitDIE(DIE &Die) { |
| 223 | MS->switchSection(Section: MOFI->getDwarfInfoSection()); |
| 224 | Asm->emitDwarfDIE(Die); |
| 225 | DebugInfoSectionSize += Die.getSize(); |
| 226 | } |
| 227 | |
| 228 | /// Emit contents of section SecName From Obj. |
| 229 | void DwarfStreamer::emitSectionContents(StringRef SecData, |
| 230 | DebugSectionKind SecKind) { |
| 231 | if (SecData.empty()) |
| 232 | return; |
| 233 | |
| 234 | if (MCSection *Section = getMCSection(SecKind)) { |
| 235 | MS->switchSection(Section); |
| 236 | |
| 237 | MS->emitBytes(Data: SecData); |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | MCSection *DwarfStreamer::getMCSection(DebugSectionKind SecKind) { |
| 242 | switch (SecKind) { |
| 243 | case DebugSectionKind::DebugInfo: |
| 244 | return MC->getObjectFileInfo()->getDwarfInfoSection(); |
| 245 | case DebugSectionKind::DebugLine: |
| 246 | return MC->getObjectFileInfo()->getDwarfLineSection(); |
| 247 | case DebugSectionKind::DebugFrame: |
| 248 | return MC->getObjectFileInfo()->getDwarfFrameSection(); |
| 249 | case DebugSectionKind::DebugRange: |
| 250 | return MC->getObjectFileInfo()->getDwarfRangesSection(); |
| 251 | case DebugSectionKind::DebugRngLists: |
| 252 | return MC->getObjectFileInfo()->getDwarfRnglistsSection(); |
| 253 | case DebugSectionKind::DebugLoc: |
| 254 | return MC->getObjectFileInfo()->getDwarfLocSection(); |
| 255 | case DebugSectionKind::DebugLocLists: |
| 256 | return MC->getObjectFileInfo()->getDwarfLoclistsSection(); |
| 257 | case DebugSectionKind::DebugARanges: |
| 258 | return MC->getObjectFileInfo()->getDwarfARangesSection(); |
| 259 | case DebugSectionKind::DebugAbbrev: |
| 260 | return MC->getObjectFileInfo()->getDwarfAbbrevSection(); |
| 261 | case DebugSectionKind::DebugMacinfo: |
| 262 | return MC->getObjectFileInfo()->getDwarfMacinfoSection(); |
| 263 | case DebugSectionKind::DebugMacro: |
| 264 | return MC->getObjectFileInfo()->getDwarfMacroSection(); |
| 265 | case DebugSectionKind::DebugAddr: |
| 266 | return MC->getObjectFileInfo()->getDwarfAddrSection(); |
| 267 | case DebugSectionKind::DebugStr: |
| 268 | return MC->getObjectFileInfo()->getDwarfStrSection(); |
| 269 | case DebugSectionKind::DebugLineStr: |
| 270 | return MC->getObjectFileInfo()->getDwarfLineStrSection(); |
| 271 | case DebugSectionKind::DebugStrOffsets: |
| 272 | return MC->getObjectFileInfo()->getDwarfStrOffSection(); |
| 273 | case DebugSectionKind::DebugPubNames: |
| 274 | return MC->getObjectFileInfo()->getDwarfPubNamesSection(); |
| 275 | case DebugSectionKind::DebugPubTypes: |
| 276 | return MC->getObjectFileInfo()->getDwarfPubTypesSection(); |
| 277 | case DebugSectionKind::DebugNames: |
| 278 | return MC->getObjectFileInfo()->getDwarfDebugNamesSection(); |
| 279 | case DebugSectionKind::AppleNames: |
| 280 | return MC->getObjectFileInfo()->getDwarfAccelNamesSection(); |
| 281 | case DebugSectionKind::AppleNamespaces: |
| 282 | return MC->getObjectFileInfo()->getDwarfAccelNamespaceSection(); |
| 283 | case DebugSectionKind::AppleObjC: |
| 284 | return MC->getObjectFileInfo()->getDwarfAccelObjCSection(); |
| 285 | case DebugSectionKind::AppleTypes: |
| 286 | return MC->getObjectFileInfo()->getDwarfAccelTypesSection(); |
| 287 | case DebugSectionKind::NumberOfEnumEntries: |
| 288 | llvm_unreachable("Unknown DebugSectionKind value" ); |
| 289 | break; |
| 290 | } |
| 291 | |
| 292 | return nullptr; |
| 293 | } |
| 294 | |
| 295 | /// Emit the debug_str section stored in \p Pool. |
| 296 | void DwarfStreamer::emitStrings(const NonRelocatableStringpool &Pool) { |
| 297 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfStrSection()); |
| 298 | std::vector<DwarfStringPoolEntryRef> Entries = Pool.getEntriesForEmission(); |
| 299 | for (auto Entry : Entries) { |
| 300 | // Emit the string itself. |
| 301 | Asm->OutStreamer->emitBytes(Data: Entry.getString()); |
| 302 | // Emit a null terminator. |
| 303 | Asm->emitInt8(Value: 0); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /// Emit the debug string offset table described by \p StringOffsets into the |
| 308 | /// .debug_str_offsets table. |
| 309 | void DwarfStreamer::emitStringOffsets( |
| 310 | const SmallVector<uint64_t> &StringOffsets, uint16_t TargetDWARFVersion) { |
| 311 | |
| 312 | if (TargetDWARFVersion < 5 || StringOffsets.empty()) |
| 313 | return; |
| 314 | |
| 315 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfStrOffSection()); |
| 316 | |
| 317 | MCSymbol *BeginLabel = Asm->createTempSymbol(Name: "Bdebugstroff" ); |
| 318 | MCSymbol *EndLabel = Asm->createTempSymbol(Name: "Edebugstroff" ); |
| 319 | |
| 320 | // Length. |
| 321 | Asm->emitLabelDifference(Hi: EndLabel, Lo: BeginLabel, Size: sizeof(uint32_t)); |
| 322 | Asm->OutStreamer->emitLabel(Symbol: BeginLabel); |
| 323 | StrOffsetSectionSize += sizeof(uint32_t); |
| 324 | |
| 325 | // Version. |
| 326 | MS->emitInt16(Value: 5); |
| 327 | StrOffsetSectionSize += sizeof(uint16_t); |
| 328 | |
| 329 | // Padding. |
| 330 | MS->emitInt16(Value: 0); |
| 331 | StrOffsetSectionSize += sizeof(uint16_t); |
| 332 | |
| 333 | for (auto Off : StringOffsets) { |
| 334 | Asm->OutStreamer->emitInt32(Value: Off); |
| 335 | StrOffsetSectionSize += sizeof(uint32_t); |
| 336 | } |
| 337 | Asm->OutStreamer->emitLabel(Symbol: EndLabel); |
| 338 | } |
| 339 | |
| 340 | /// Emit the debug_line_str section stored in \p Pool. |
| 341 | void DwarfStreamer::emitLineStrings(const NonRelocatableStringpool &Pool) { |
| 342 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfLineStrSection()); |
| 343 | std::vector<DwarfStringPoolEntryRef> Entries = Pool.getEntriesForEmission(); |
| 344 | for (auto Entry : Entries) { |
| 345 | // Emit the string itself. |
| 346 | Asm->OutStreamer->emitBytes(Data: Entry.getString()); |
| 347 | // Emit a null terminator. |
| 348 | Asm->emitInt8(Value: 0); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | void DwarfStreamer::emitDebugNames(DWARF5AccelTable &Table) { |
| 353 | if (EmittedUnits.empty()) |
| 354 | return; |
| 355 | |
| 356 | // Build up data structures needed to emit this section. |
| 357 | std::vector<std::variant<MCSymbol *, uint64_t>> CompUnits; |
| 358 | DenseMap<unsigned, unsigned> UniqueIdToCuMap; |
| 359 | unsigned Id = 0; |
| 360 | for (auto &CU : EmittedUnits) { |
| 361 | CompUnits.push_back(x: CU.LabelBegin); |
| 362 | // We might be omitting CUs, so we need to remap them. |
| 363 | UniqueIdToCuMap[CU.ID] = Id++; |
| 364 | } |
| 365 | |
| 366 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfDebugNamesSection()); |
| 367 | dwarf::Form Form = DIEInteger::BestForm(/*IsSigned*/ false, |
| 368 | Int: (uint64_t)UniqueIdToCuMap.size() - 1); |
| 369 | /// llvm-dwarfutil doesn't support type units + .debug_names right now. |
| 370 | // FIXME: add support for type units + .debug_names. For now the behavior is |
| 371 | // unsuported. |
| 372 | emitDWARF5AccelTable( |
| 373 | Asm: Asm.get(), Contents&: Table, CUs: CompUnits, |
| 374 | getIndexForEntry: [&](const DWARF5AccelTableData &Entry) |
| 375 | -> std::optional<DWARF5AccelTable::UnitIndexAndEncoding> { |
| 376 | if (UniqueIdToCuMap.size() > 1) |
| 377 | return {{.Index: UniqueIdToCuMap[Entry.getUnitID()], |
| 378 | .Encoding: {.Index: dwarf::DW_IDX_compile_unit, .Form: Form}}}; |
| 379 | return std::nullopt; |
| 380 | }); |
| 381 | } |
| 382 | |
| 383 | void DwarfStreamer::emitAppleNamespaces( |
| 384 | AccelTable<AppleAccelTableStaticOffsetData> &Table) { |
| 385 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfAccelNamespaceSection()); |
| 386 | auto *SectionBegin = Asm->createTempSymbol(Name: "namespac_begin" ); |
| 387 | Asm->OutStreamer->emitLabel(Symbol: SectionBegin); |
| 388 | emitAppleAccelTable(Asm: Asm.get(), Contents&: Table, Prefix: "namespac" , SecBegin: SectionBegin); |
| 389 | } |
| 390 | |
| 391 | void DwarfStreamer::emitAppleNames( |
| 392 | AccelTable<AppleAccelTableStaticOffsetData> &Table) { |
| 393 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfAccelNamesSection()); |
| 394 | auto *SectionBegin = Asm->createTempSymbol(Name: "names_begin" ); |
| 395 | Asm->OutStreamer->emitLabel(Symbol: SectionBegin); |
| 396 | emitAppleAccelTable(Asm: Asm.get(), Contents&: Table, Prefix: "names" , SecBegin: SectionBegin); |
| 397 | } |
| 398 | |
| 399 | void DwarfStreamer::emitAppleObjc( |
| 400 | AccelTable<AppleAccelTableStaticOffsetData> &Table) { |
| 401 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfAccelObjCSection()); |
| 402 | auto *SectionBegin = Asm->createTempSymbol(Name: "objc_begin" ); |
| 403 | Asm->OutStreamer->emitLabel(Symbol: SectionBegin); |
| 404 | emitAppleAccelTable(Asm: Asm.get(), Contents&: Table, Prefix: "objc" , SecBegin: SectionBegin); |
| 405 | } |
| 406 | |
| 407 | void DwarfStreamer::emitAppleTypes( |
| 408 | AccelTable<AppleAccelTableStaticTypeData> &Table) { |
| 409 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfAccelTypesSection()); |
| 410 | auto *SectionBegin = Asm->createTempSymbol(Name: "types_begin" ); |
| 411 | Asm->OutStreamer->emitLabel(Symbol: SectionBegin); |
| 412 | emitAppleAccelTable(Asm: Asm.get(), Contents&: Table, Prefix: "types" , SecBegin: SectionBegin); |
| 413 | } |
| 414 | |
| 415 | /// Emit the swift_ast section stored in \p Buffers. |
| 416 | void DwarfStreamer::emitSwiftAST(StringRef Buffer) { |
| 417 | MCSection *SwiftASTSection = MOFI->getDwarfSwiftASTSection(); |
| 418 | SwiftASTSection->setAlignment(Align(32)); |
| 419 | MS->switchSection(Section: SwiftASTSection); |
| 420 | MS->emitBytes(Data: Buffer); |
| 421 | } |
| 422 | |
| 423 | void DwarfStreamer::emitSwiftReflectionSection( |
| 424 | llvm::binaryformat::Swift5ReflectionSectionKind ReflSectionKind, |
| 425 | StringRef Buffer, uint32_t Alignment, uint32_t Size) { |
| 426 | MCSection *ReflectionSection = |
| 427 | MOFI->getSwift5ReflectionSection(ReflSectionKind); |
| 428 | if (ReflectionSection == nullptr) |
| 429 | return; |
| 430 | ReflectionSection->setAlignment(Align(Alignment)); |
| 431 | MS->switchSection(Section: ReflectionSection); |
| 432 | MS->emitBytes(Data: Buffer); |
| 433 | } |
| 434 | |
| 435 | void DwarfStreamer::emitDwarfDebugArangesTable( |
| 436 | const CompileUnit &Unit, const AddressRanges &LinkedRanges) { |
| 437 | unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize(); |
| 438 | |
| 439 | // Make .debug_aranges to be current section. |
| 440 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfARangesSection()); |
| 441 | |
| 442 | // Emit Header. |
| 443 | MCSymbol *BeginLabel = Asm->createTempSymbol(Name: "Barange" ); |
| 444 | MCSymbol *EndLabel = Asm->createTempSymbol(Name: "Earange" ); |
| 445 | |
| 446 | unsigned = |
| 447 | sizeof(int32_t) + // Size of contents (w/o this field |
| 448 | sizeof(int16_t) + // DWARF ARange version number |
| 449 | sizeof(int32_t) + // Offset of CU in the .debug_info section |
| 450 | sizeof(int8_t) + // Pointer Size (in bytes) |
| 451 | sizeof(int8_t); // Segment Size (in bytes) |
| 452 | |
| 453 | unsigned TupleSize = AddressSize * 2; |
| 454 | unsigned Padding = offsetToAlignment(Value: HeaderSize, Alignment: Align(TupleSize)); |
| 455 | |
| 456 | Asm->emitLabelDifference(Hi: EndLabel, Lo: BeginLabel, Size: 4); // Arange length |
| 457 | Asm->OutStreamer->emitLabel(Symbol: BeginLabel); |
| 458 | Asm->emitInt16(Value: dwarf::DW_ARANGES_VERSION); // Version number |
| 459 | Asm->emitInt32(Value: Unit.getStartOffset()); // Corresponding unit's offset |
| 460 | Asm->emitInt8(Value: AddressSize); // Address size |
| 461 | Asm->emitInt8(Value: 0); // Segment size |
| 462 | |
| 463 | Asm->OutStreamer->emitFill(NumBytes: Padding, FillValue: 0x0); |
| 464 | |
| 465 | // Emit linked ranges. |
| 466 | for (const AddressRange &Range : LinkedRanges) { |
| 467 | MS->emitIntValue(Value: Range.start(), Size: AddressSize); |
| 468 | MS->emitIntValue(Value: Range.end() - Range.start(), Size: AddressSize); |
| 469 | } |
| 470 | |
| 471 | // Emit terminator. |
| 472 | Asm->OutStreamer->emitIntValue(Value: 0, Size: AddressSize); |
| 473 | Asm->OutStreamer->emitIntValue(Value: 0, Size: AddressSize); |
| 474 | Asm->OutStreamer->emitLabel(Symbol: EndLabel); |
| 475 | } |
| 476 | |
| 477 | Error DwarfStreamer::emitDwarfDebugRangesTableFragment( |
| 478 | const CompileUnit &Unit, const AddressRanges &LinkedRanges, |
| 479 | PatchLocation Patch) { |
| 480 | Expected<uint64_t> Offset = clampSecOffset( |
| 481 | Offset: RangesSectionSize, FP: Unit.getOrigUnit().getFormParams(), Section: ".debug_ranges" ); |
| 482 | if (!Offset) |
| 483 | return Offset.takeError(); |
| 484 | Patch.set(*Offset); |
| 485 | |
| 486 | // Make .debug_ranges to be current section. |
| 487 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfRangesSection()); |
| 488 | unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize(); |
| 489 | |
| 490 | // Emit ranges. |
| 491 | uint64_t BaseAddress = 0; |
| 492 | if (std::optional<uint64_t> LowPC = Unit.getLowPc()) |
| 493 | BaseAddress = *LowPC; |
| 494 | |
| 495 | for (const AddressRange &Range : LinkedRanges) { |
| 496 | MS->emitIntValue(Value: Range.start() - BaseAddress, Size: AddressSize); |
| 497 | MS->emitIntValue(Value: Range.end() - BaseAddress, Size: AddressSize); |
| 498 | |
| 499 | RangesSectionSize += AddressSize; |
| 500 | RangesSectionSize += AddressSize; |
| 501 | } |
| 502 | |
| 503 | // Add the terminator entry. |
| 504 | MS->emitIntValue(Value: 0, Size: AddressSize); |
| 505 | MS->emitIntValue(Value: 0, Size: AddressSize); |
| 506 | |
| 507 | RangesSectionSize += AddressSize; |
| 508 | RangesSectionSize += AddressSize; |
| 509 | return Error::success(); |
| 510 | } |
| 511 | |
| 512 | MCSymbol * |
| 513 | DwarfStreamer::(const CompileUnit &Unit) { |
| 514 | if (Unit.getOrigUnit().getVersion() < 5) |
| 515 | return nullptr; |
| 516 | |
| 517 | // Make .debug_rnglists to be current section. |
| 518 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfRnglistsSection()); |
| 519 | |
| 520 | MCSymbol *BeginLabel = Asm->createTempSymbol(Name: "Brnglists" ); |
| 521 | MCSymbol *EndLabel = Asm->createTempSymbol(Name: "Ernglists" ); |
| 522 | unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize(); |
| 523 | |
| 524 | // Length |
| 525 | Asm->emitLabelDifference(Hi: EndLabel, Lo: BeginLabel, Size: sizeof(uint32_t)); |
| 526 | Asm->OutStreamer->emitLabel(Symbol: BeginLabel); |
| 527 | RngListsSectionSize += sizeof(uint32_t); |
| 528 | |
| 529 | // Version. |
| 530 | MS->emitInt16(Value: 5); |
| 531 | RngListsSectionSize += sizeof(uint16_t); |
| 532 | |
| 533 | // Address size. |
| 534 | MS->emitInt8(Value: AddressSize); |
| 535 | RngListsSectionSize++; |
| 536 | |
| 537 | // Seg_size |
| 538 | MS->emitInt8(Value: 0); |
| 539 | RngListsSectionSize++; |
| 540 | |
| 541 | // Offset entry count |
| 542 | MS->emitInt32(Value: 0); |
| 543 | RngListsSectionSize += sizeof(uint32_t); |
| 544 | |
| 545 | return EndLabel; |
| 546 | } |
| 547 | |
| 548 | Error DwarfStreamer::emitDwarfDebugRangeListFragment( |
| 549 | const CompileUnit &Unit, const AddressRanges &LinkedRanges, |
| 550 | PatchLocation Patch, DebugDieValuePool &AddrPool) { |
| 551 | if (Unit.getOrigUnit().getVersion() < 5) { |
| 552 | return emitDwarfDebugRangesTableFragment(Unit, LinkedRanges, Patch); |
| 553 | } |
| 554 | |
| 555 | return emitDwarfDebugRngListsTableFragment(Unit, LinkedRanges, Patch, |
| 556 | AddrPool); |
| 557 | } |
| 558 | |
| 559 | void DwarfStreamer::(const CompileUnit &Unit, |
| 560 | MCSymbol *EndLabel) { |
| 561 | if (Unit.getOrigUnit().getVersion() < 5) |
| 562 | return; |
| 563 | |
| 564 | // Make .debug_rnglists to be current section. |
| 565 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfRnglistsSection()); |
| 566 | |
| 567 | if (EndLabel != nullptr) |
| 568 | Asm->OutStreamer->emitLabel(Symbol: EndLabel); |
| 569 | } |
| 570 | |
| 571 | Error DwarfStreamer::emitDwarfDebugRngListsTableFragment( |
| 572 | const CompileUnit &Unit, const AddressRanges &LinkedRanges, |
| 573 | PatchLocation Patch, DebugDieValuePool &AddrPool) { |
| 574 | Expected<uint64_t> Offset = |
| 575 | clampSecOffset(Offset: RngListsSectionSize, FP: Unit.getOrigUnit().getFormParams(), |
| 576 | Section: ".debug_rnglists" ); |
| 577 | if (!Offset) |
| 578 | return Offset.takeError(); |
| 579 | Patch.set(*Offset); |
| 580 | |
| 581 | // Make .debug_rnglists to be current section. |
| 582 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfRnglistsSection()); |
| 583 | std::optional<uint64_t> BaseAddress; |
| 584 | |
| 585 | for (const AddressRange &Range : LinkedRanges) { |
| 586 | |
| 587 | if (!BaseAddress) { |
| 588 | BaseAddress = Range.start(); |
| 589 | |
| 590 | // Emit base address. |
| 591 | MS->emitInt8(Value: dwarf::DW_RLE_base_addressx); |
| 592 | RngListsSectionSize += 1; |
| 593 | RngListsSectionSize += |
| 594 | MS->emitULEB128IntValue(Value: AddrPool.getValueIndex(Value: *BaseAddress)); |
| 595 | } |
| 596 | |
| 597 | // Emit type of entry. |
| 598 | MS->emitInt8(Value: dwarf::DW_RLE_offset_pair); |
| 599 | RngListsSectionSize += 1; |
| 600 | |
| 601 | // Emit start offset relative to base address. |
| 602 | RngListsSectionSize += |
| 603 | MS->emitULEB128IntValue(Value: Range.start() - *BaseAddress); |
| 604 | |
| 605 | // Emit end offset relative to base address. |
| 606 | RngListsSectionSize += MS->emitULEB128IntValue(Value: Range.end() - *BaseAddress); |
| 607 | } |
| 608 | |
| 609 | // Emit the terminator entry. |
| 610 | MS->emitInt8(Value: dwarf::DW_RLE_end_of_list); |
| 611 | RngListsSectionSize += 1; |
| 612 | return Error::success(); |
| 613 | } |
| 614 | |
| 615 | /// Emit debug locations(.debug_loc, .debug_loclists) header. |
| 616 | MCSymbol *DwarfStreamer::(const CompileUnit &Unit) { |
| 617 | if (Unit.getOrigUnit().getVersion() < 5) |
| 618 | return nullptr; |
| 619 | |
| 620 | // Make .debug_loclists the current section. |
| 621 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfLoclistsSection()); |
| 622 | |
| 623 | MCSymbol *BeginLabel = Asm->createTempSymbol(Name: "Bloclists" ); |
| 624 | MCSymbol *EndLabel = Asm->createTempSymbol(Name: "Eloclists" ); |
| 625 | unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize(); |
| 626 | |
| 627 | // Length |
| 628 | Asm->emitLabelDifference(Hi: EndLabel, Lo: BeginLabel, Size: sizeof(uint32_t)); |
| 629 | Asm->OutStreamer->emitLabel(Symbol: BeginLabel); |
| 630 | LocListsSectionSize += sizeof(uint32_t); |
| 631 | |
| 632 | // Version. |
| 633 | MS->emitInt16(Value: 5); |
| 634 | LocListsSectionSize += sizeof(uint16_t); |
| 635 | |
| 636 | // Address size. |
| 637 | MS->emitInt8(Value: AddressSize); |
| 638 | LocListsSectionSize++; |
| 639 | |
| 640 | // Seg_size |
| 641 | MS->emitInt8(Value: 0); |
| 642 | LocListsSectionSize++; |
| 643 | |
| 644 | // Offset entry count |
| 645 | MS->emitInt32(Value: 0); |
| 646 | LocListsSectionSize += sizeof(uint32_t); |
| 647 | |
| 648 | return EndLabel; |
| 649 | } |
| 650 | |
| 651 | /// Emit debug locations(.debug_loc, .debug_loclists) fragment. |
| 652 | Error DwarfStreamer::emitDwarfDebugLocListFragment( |
| 653 | const CompileUnit &Unit, |
| 654 | const DWARFLocationExpressionsVector &LinkedLocationExpression, |
| 655 | PatchLocation Patch, DebugDieValuePool &AddrPool) { |
| 656 | if (Unit.getOrigUnit().getVersion() < 5) { |
| 657 | return emitDwarfDebugLocTableFragment(Unit, LinkedLocationExpression, |
| 658 | Patch); |
| 659 | } |
| 660 | |
| 661 | return emitDwarfDebugLocListsTableFragment(Unit, LinkedLocationExpression, |
| 662 | Patch, AddrPool); |
| 663 | } |
| 664 | |
| 665 | /// Emit debug locations(.debug_loc, .debug_loclists) footer. |
| 666 | void DwarfStreamer::(const CompileUnit &Unit, |
| 667 | MCSymbol *EndLabel) { |
| 668 | if (Unit.getOrigUnit().getVersion() < 5) |
| 669 | return; |
| 670 | |
| 671 | // Make .debug_loclists the current section. |
| 672 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfLoclistsSection()); |
| 673 | |
| 674 | if (EndLabel != nullptr) |
| 675 | Asm->OutStreamer->emitLabel(Symbol: EndLabel); |
| 676 | } |
| 677 | |
| 678 | /// Emit piece of .debug_loc for \p LinkedLocationExpression. |
| 679 | Error DwarfStreamer::emitDwarfDebugLocTableFragment( |
| 680 | const CompileUnit &Unit, |
| 681 | const DWARFLocationExpressionsVector &LinkedLocationExpression, |
| 682 | PatchLocation Patch) { |
| 683 | Expected<uint64_t> Offset = clampSecOffset( |
| 684 | Offset: LocSectionSize, FP: Unit.getOrigUnit().getFormParams(), Section: ".debug_loc" ); |
| 685 | if (!Offset) |
| 686 | return Offset.takeError(); |
| 687 | Patch.set(*Offset); |
| 688 | |
| 689 | // Make .debug_loc to be current section. |
| 690 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfLocSection()); |
| 691 | unsigned AddressSize = Unit.getOrigUnit().getAddressByteSize(); |
| 692 | |
| 693 | // Emit ranges. |
| 694 | uint64_t BaseAddress = 0; |
| 695 | if (std::optional<uint64_t> LowPC = Unit.getLowPc()) |
| 696 | BaseAddress = *LowPC; |
| 697 | |
| 698 | for (const DWARFLocationExpression &LocExpression : |
| 699 | LinkedLocationExpression) { |
| 700 | if (LocExpression.Range) { |
| 701 | MS->emitIntValue(Value: LocExpression.Range->LowPC - BaseAddress, Size: AddressSize); |
| 702 | MS->emitIntValue(Value: LocExpression.Range->HighPC - BaseAddress, Size: AddressSize); |
| 703 | |
| 704 | LocSectionSize += AddressSize; |
| 705 | LocSectionSize += AddressSize; |
| 706 | } |
| 707 | |
| 708 | Asm->OutStreamer->emitIntValue(Value: LocExpression.Expr.size(), Size: 2); |
| 709 | Asm->OutStreamer->emitBytes(Data: StringRef( |
| 710 | (const char *)LocExpression.Expr.data(), LocExpression.Expr.size())); |
| 711 | LocSectionSize += LocExpression.Expr.size() + 2; |
| 712 | } |
| 713 | |
| 714 | // Add the terminator entry. |
| 715 | MS->emitIntValue(Value: 0, Size: AddressSize); |
| 716 | MS->emitIntValue(Value: 0, Size: AddressSize); |
| 717 | |
| 718 | LocSectionSize += AddressSize; |
| 719 | LocSectionSize += AddressSize; |
| 720 | return Error::success(); |
| 721 | } |
| 722 | |
| 723 | /// Emit .debug_addr header. |
| 724 | MCSymbol *DwarfStreamer::(const CompileUnit &Unit) { |
| 725 | |
| 726 | // Make .debug_addr the current section. |
| 727 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfAddrSection()); |
| 728 | |
| 729 | MCSymbol *BeginLabel = Asm->createTempSymbol(Name: "Bdebugaddr" ); |
| 730 | MCSymbol *EndLabel = Asm->createTempSymbol(Name: "Edebugaddr" ); |
| 731 | unsigned AddrSize = Unit.getOrigUnit().getAddressByteSize(); |
| 732 | |
| 733 | // Emit length. |
| 734 | Asm->emitLabelDifference(Hi: EndLabel, Lo: BeginLabel, Size: sizeof(uint32_t)); |
| 735 | Asm->OutStreamer->emitLabel(Symbol: BeginLabel); |
| 736 | AddrSectionSize += sizeof(uint32_t); |
| 737 | |
| 738 | // Emit version. |
| 739 | Asm->emitInt16(Value: 5); |
| 740 | AddrSectionSize += 2; |
| 741 | |
| 742 | // Emit address size. |
| 743 | Asm->emitInt8(Value: AddrSize); |
| 744 | AddrSectionSize += 1; |
| 745 | |
| 746 | // Emit segment size. |
| 747 | Asm->emitInt8(Value: 0); |
| 748 | AddrSectionSize += 1; |
| 749 | |
| 750 | return EndLabel; |
| 751 | } |
| 752 | |
| 753 | /// Emit the .debug_addr addresses stored in \p Addrs. |
| 754 | void DwarfStreamer::emitDwarfDebugAddrs(const SmallVector<uint64_t> &Addrs, |
| 755 | uint8_t AddrSize) { |
| 756 | Asm->OutStreamer->switchSection(Section: MOFI->getDwarfAddrSection()); |
| 757 | for (auto Addr : Addrs) { |
| 758 | Asm->OutStreamer->emitIntValue(Value: Addr, Size: AddrSize); |
| 759 | AddrSectionSize += AddrSize; |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /// Emit .debug_addr footer. |
| 764 | void DwarfStreamer::(const CompileUnit &Unit, |
| 765 | MCSymbol *EndLabel) { |
| 766 | |
| 767 | // Make .debug_addr the current section. |
| 768 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfAddrSection()); |
| 769 | |
| 770 | if (EndLabel != nullptr) |
| 771 | Asm->OutStreamer->emitLabel(Symbol: EndLabel); |
| 772 | } |
| 773 | |
| 774 | /// Emit piece of .debug_loclists for \p LinkedLocationExpression. |
| 775 | Error DwarfStreamer::emitDwarfDebugLocListsTableFragment( |
| 776 | const CompileUnit &Unit, |
| 777 | const DWARFLocationExpressionsVector &LinkedLocationExpression, |
| 778 | PatchLocation Patch, DebugDieValuePool &AddrPool) { |
| 779 | Expected<uint64_t> Offset = |
| 780 | clampSecOffset(Offset: LocListsSectionSize, FP: Unit.getOrigUnit().getFormParams(), |
| 781 | Section: ".debug_loclists" ); |
| 782 | if (!Offset) |
| 783 | return Offset.takeError(); |
| 784 | Patch.set(*Offset); |
| 785 | |
| 786 | // Make .debug_loclists the current section. |
| 787 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfLoclistsSection()); |
| 788 | std::optional<uint64_t> BaseAddress; |
| 789 | |
| 790 | for (const DWARFLocationExpression &LocExpression : |
| 791 | LinkedLocationExpression) { |
| 792 | if (LocExpression.Range) { |
| 793 | |
| 794 | if (!BaseAddress) { |
| 795 | |
| 796 | BaseAddress = LocExpression.Range->LowPC; |
| 797 | |
| 798 | // Emit base address. |
| 799 | MS->emitInt8(Value: dwarf::DW_LLE_base_addressx); |
| 800 | LocListsSectionSize += 1; |
| 801 | LocListsSectionSize += |
| 802 | MS->emitULEB128IntValue(Value: AddrPool.getValueIndex(Value: *BaseAddress)); |
| 803 | } |
| 804 | |
| 805 | // Emit type of entry. |
| 806 | MS->emitInt8(Value: dwarf::DW_LLE_offset_pair); |
| 807 | LocListsSectionSize += 1; |
| 808 | |
| 809 | // Emit start offset relative to base address. |
| 810 | LocListsSectionSize += |
| 811 | MS->emitULEB128IntValue(Value: LocExpression.Range->LowPC - *BaseAddress); |
| 812 | |
| 813 | // Emit end offset relative to base address. |
| 814 | LocListsSectionSize += |
| 815 | MS->emitULEB128IntValue(Value: LocExpression.Range->HighPC - *BaseAddress); |
| 816 | } else { |
| 817 | // Emit type of entry. |
| 818 | MS->emitInt8(Value: dwarf::DW_LLE_default_location); |
| 819 | LocListsSectionSize += 1; |
| 820 | } |
| 821 | |
| 822 | LocListsSectionSize += MS->emitULEB128IntValue(Value: LocExpression.Expr.size()); |
| 823 | Asm->OutStreamer->emitBytes(Data: StringRef( |
| 824 | (const char *)LocExpression.Expr.data(), LocExpression.Expr.size())); |
| 825 | LocListsSectionSize += LocExpression.Expr.size(); |
| 826 | } |
| 827 | |
| 828 | // Emit the terminator entry. |
| 829 | MS->emitInt8(Value: dwarf::DW_LLE_end_of_list); |
| 830 | LocListsSectionSize += 1; |
| 831 | return Error::success(); |
| 832 | } |
| 833 | |
| 834 | void DwarfStreamer::emitLineTableForUnit( |
| 835 | const DWARFDebugLine::LineTable &LineTable, const CompileUnit &Unit, |
| 836 | OffsetsStringPool &DebugStrPool, OffsetsStringPool &DebugLineStrPool, |
| 837 | std::vector<uint64_t> *RowOffsets) { |
| 838 | // Switch to the section where the table will be emitted into. |
| 839 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfLineSection()); |
| 840 | |
| 841 | MCSymbol *LineStartSym = MC->createTempSymbol(); |
| 842 | MCSymbol *LineEndSym = MC->createTempSymbol(); |
| 843 | |
| 844 | // unit_length. |
| 845 | if (LineTable.Prologue.FormParams.Format == dwarf::DwarfFormat::DWARF64) { |
| 846 | MS->emitInt32(Value: dwarf::DW_LENGTH_DWARF64); |
| 847 | LineSectionSize += 4; |
| 848 | } |
| 849 | emitLabelDifference(Hi: LineEndSym, Lo: LineStartSym, |
| 850 | Format: LineTable.Prologue.FormParams.Format, SectionSize&: LineSectionSize); |
| 851 | Asm->OutStreamer->emitLabel(Symbol: LineStartSym); |
| 852 | |
| 853 | // Emit prologue. |
| 854 | emitLineTablePrologue(P: LineTable.Prologue, DebugStrPool, DebugLineStrPool); |
| 855 | |
| 856 | // Emit rows. |
| 857 | emitLineTableRows(LineTable, LineEndSym, |
| 858 | AddressByteSize: Unit.getOrigUnit().getAddressByteSize(), RowOffsets); |
| 859 | } |
| 860 | |
| 861 | void DwarfStreamer::emitLineTablePrologue(const DWARFDebugLine::Prologue &P, |
| 862 | OffsetsStringPool &DebugStrPool, |
| 863 | OffsetsStringPool &DebugLineStrPool) { |
| 864 | MCSymbol *PrologueStartSym = MC->createTempSymbol(); |
| 865 | MCSymbol *PrologueEndSym = MC->createTempSymbol(); |
| 866 | |
| 867 | // version (uhalf). |
| 868 | MS->emitInt16(Value: P.getVersion()); |
| 869 | LineSectionSize += 2; |
| 870 | if (P.getVersion() == 5) { |
| 871 | // address_size (ubyte). |
| 872 | MS->emitInt8(Value: P.getAddressSize()); |
| 873 | LineSectionSize += 1; |
| 874 | |
| 875 | // segment_selector_size (ubyte). |
| 876 | MS->emitInt8(Value: P.SegSelectorSize); |
| 877 | LineSectionSize += 1; |
| 878 | } |
| 879 | |
| 880 | // header_length. |
| 881 | emitLabelDifference(Hi: PrologueEndSym, Lo: PrologueStartSym, Format: P.FormParams.Format, |
| 882 | SectionSize&: LineSectionSize); |
| 883 | |
| 884 | Asm->OutStreamer->emitLabel(Symbol: PrologueStartSym); |
| 885 | emitLineTableProloguePayload(P, DebugStrPool, DebugLineStrPool); |
| 886 | Asm->OutStreamer->emitLabel(Symbol: PrologueEndSym); |
| 887 | } |
| 888 | |
| 889 | void DwarfStreamer::emitLineTablePrologueV2IncludeAndFileTable( |
| 890 | const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool, |
| 891 | OffsetsStringPool &DebugLineStrPool) { |
| 892 | // include_directories (sequence of path names). |
| 893 | for (const DWARFFormValue &Include : P.IncludeDirectories) |
| 894 | emitLineTableString(P, String: Include, DebugStrPool, DebugLineStrPool); |
| 895 | // The last entry is followed by a single null byte. |
| 896 | MS->emitInt8(Value: 0); |
| 897 | LineSectionSize += 1; |
| 898 | |
| 899 | // file_names (sequence of file entries). |
| 900 | for (const DWARFDebugLine::FileNameEntry &File : P.FileNames) { |
| 901 | // A null-terminated string containing the full or relative path name of a |
| 902 | // source file. |
| 903 | emitLineTableString(P, String: File.Name, DebugStrPool, DebugLineStrPool); |
| 904 | // An unsigned LEB128 number representing the directory index of a directory |
| 905 | // in the include_directories section. |
| 906 | LineSectionSize += MS->emitULEB128IntValue(Value: File.DirIdx); |
| 907 | // An unsigned LEB128 number representing the (implementation-defined) time |
| 908 | // of last modification for the file, or 0 if not available. |
| 909 | LineSectionSize += MS->emitULEB128IntValue(Value: File.ModTime); |
| 910 | // An unsigned LEB128 number representing the length in bytes of the file, |
| 911 | // or 0 if not available. |
| 912 | LineSectionSize += MS->emitULEB128IntValue(Value: File.Length); |
| 913 | } |
| 914 | // The last entry is followed by a single null byte. |
| 915 | MS->emitInt8(Value: 0); |
| 916 | LineSectionSize += 1; |
| 917 | } |
| 918 | |
| 919 | void DwarfStreamer::emitLineTablePrologueV5IncludeAndFileTable( |
| 920 | const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool, |
| 921 | OffsetsStringPool &DebugLineStrPool) { |
| 922 | if (P.IncludeDirectories.empty()) { |
| 923 | // directory_entry_format_count(ubyte). |
| 924 | MS->emitInt8(Value: 0); |
| 925 | LineSectionSize += 1; |
| 926 | } else { |
| 927 | // directory_entry_format_count(ubyte). |
| 928 | MS->emitInt8(Value: 1); |
| 929 | LineSectionSize += 1; |
| 930 | |
| 931 | // directory_entry_format (sequence of ULEB128 pairs). |
| 932 | LineSectionSize += MS->emitULEB128IntValue(Value: dwarf::DW_LNCT_path); |
| 933 | LineSectionSize += |
| 934 | MS->emitULEB128IntValue(Value: P.IncludeDirectories[0].getForm()); |
| 935 | } |
| 936 | |
| 937 | // directories_count (ULEB128). |
| 938 | LineSectionSize += MS->emitULEB128IntValue(Value: P.IncludeDirectories.size()); |
| 939 | // directories (sequence of directory names). |
| 940 | for (auto Include : P.IncludeDirectories) |
| 941 | emitLineTableString(P, String: Include, DebugStrPool, DebugLineStrPool); |
| 942 | |
| 943 | bool HasChecksums = P.ContentTypes.HasMD5; |
| 944 | bool HasInlineSources = P.ContentTypes.HasSource; |
| 945 | |
| 946 | if (P.FileNames.empty()) { |
| 947 | // file_name_entry_format_count (ubyte). |
| 948 | MS->emitInt8(Value: 0); |
| 949 | LineSectionSize += 1; |
| 950 | } else { |
| 951 | // file_name_entry_format_count (ubyte). |
| 952 | MS->emitInt8(Value: 2 + (HasChecksums ? 1 : 0) + (HasInlineSources ? 1 : 0)); |
| 953 | LineSectionSize += 1; |
| 954 | |
| 955 | // file_name_entry_format (sequence of ULEB128 pairs). |
| 956 | auto StrForm = P.FileNames[0].Name.getForm(); |
| 957 | LineSectionSize += MS->emitULEB128IntValue(Value: dwarf::DW_LNCT_path); |
| 958 | LineSectionSize += MS->emitULEB128IntValue(Value: StrForm); |
| 959 | |
| 960 | LineSectionSize += MS->emitULEB128IntValue(Value: dwarf::DW_LNCT_directory_index); |
| 961 | LineSectionSize += MS->emitULEB128IntValue(Value: dwarf::DW_FORM_udata); |
| 962 | |
| 963 | if (HasChecksums) { |
| 964 | LineSectionSize += MS->emitULEB128IntValue(Value: dwarf::DW_LNCT_MD5); |
| 965 | LineSectionSize += MS->emitULEB128IntValue(Value: dwarf::DW_FORM_data16); |
| 966 | } |
| 967 | |
| 968 | if (HasInlineSources) { |
| 969 | LineSectionSize += MS->emitULEB128IntValue(Value: dwarf::DW_LNCT_LLVM_source); |
| 970 | LineSectionSize += MS->emitULEB128IntValue(Value: StrForm); |
| 971 | } |
| 972 | } |
| 973 | |
| 974 | // file_names_count (ULEB128). |
| 975 | LineSectionSize += MS->emitULEB128IntValue(Value: P.FileNames.size()); |
| 976 | |
| 977 | // file_names (sequence of file name entries). |
| 978 | for (auto File : P.FileNames) { |
| 979 | emitLineTableString(P, String: File.Name, DebugStrPool, DebugLineStrPool); |
| 980 | LineSectionSize += MS->emitULEB128IntValue(Value: File.DirIdx); |
| 981 | if (HasChecksums) { |
| 982 | MS->emitBinaryData( |
| 983 | Data: StringRef(reinterpret_cast<const char *>(File.Checksum.data()), |
| 984 | File.Checksum.size())); |
| 985 | LineSectionSize += File.Checksum.size(); |
| 986 | } |
| 987 | if (HasInlineSources) |
| 988 | emitLineTableString(P, String: File.Source, DebugStrPool, DebugLineStrPool); |
| 989 | } |
| 990 | } |
| 991 | |
| 992 | void DwarfStreamer::emitLineTableString(const DWARFDebugLine::Prologue &P, |
| 993 | const DWARFFormValue &String, |
| 994 | OffsetsStringPool &DebugStrPool, |
| 995 | OffsetsStringPool &DebugLineStrPool) { |
| 996 | std::optional<const char *> StringVal = dwarf::toString(V: String); |
| 997 | if (!StringVal) { |
| 998 | warn(Warning: "Cann't read string from line table." ); |
| 999 | return; |
| 1000 | } |
| 1001 | |
| 1002 | switch (String.getForm()) { |
| 1003 | case dwarf::DW_FORM_string: { |
| 1004 | StringRef Str = *StringVal; |
| 1005 | Asm->OutStreamer->emitBytes(Data: Str.data()); |
| 1006 | Asm->emitInt8(Value: 0); |
| 1007 | LineSectionSize += Str.size() + 1; |
| 1008 | } break; |
| 1009 | case dwarf::DW_FORM_strp: |
| 1010 | case dwarf::DW_FORM_line_strp: { |
| 1011 | DwarfStringPoolEntryRef StringRef = |
| 1012 | String.getForm() == dwarf::DW_FORM_strp |
| 1013 | ? DebugStrPool.getEntry(S: *StringVal) |
| 1014 | : DebugLineStrPool.getEntry(S: *StringVal); |
| 1015 | |
| 1016 | emitIntOffset(Offset: StringRef.getOffset(), Format: P.FormParams.Format, SectionSize&: LineSectionSize); |
| 1017 | } break; |
| 1018 | default: |
| 1019 | warn(Warning: "Unsupported string form inside line table." ); |
| 1020 | break; |
| 1021 | }; |
| 1022 | } |
| 1023 | |
| 1024 | void DwarfStreamer::emitLineTableProloguePayload( |
| 1025 | const DWARFDebugLine::Prologue &P, OffsetsStringPool &DebugStrPool, |
| 1026 | OffsetsStringPool &DebugLineStrPool) { |
| 1027 | // minimum_instruction_length (ubyte). |
| 1028 | MS->emitInt8(Value: P.MinInstLength); |
| 1029 | LineSectionSize += 1; |
| 1030 | if (P.FormParams.Version >= 4) { |
| 1031 | // maximum_operations_per_instruction (ubyte). |
| 1032 | MS->emitInt8(Value: P.MaxOpsPerInst); |
| 1033 | LineSectionSize += 1; |
| 1034 | } |
| 1035 | // default_is_stmt (ubyte). |
| 1036 | MS->emitInt8(Value: P.DefaultIsStmt); |
| 1037 | LineSectionSize += 1; |
| 1038 | // line_base (sbyte). |
| 1039 | MS->emitInt8(Value: P.LineBase); |
| 1040 | LineSectionSize += 1; |
| 1041 | // line_range (ubyte). |
| 1042 | MS->emitInt8(Value: P.LineRange); |
| 1043 | LineSectionSize += 1; |
| 1044 | // opcode_base (ubyte). |
| 1045 | MS->emitInt8(Value: P.OpcodeBase); |
| 1046 | LineSectionSize += 1; |
| 1047 | |
| 1048 | // standard_opcode_lengths (array of ubyte). |
| 1049 | for (auto Length : P.StandardOpcodeLengths) { |
| 1050 | MS->emitInt8(Value: Length); |
| 1051 | LineSectionSize += 1; |
| 1052 | } |
| 1053 | |
| 1054 | if (P.FormParams.Version < 5) |
| 1055 | emitLineTablePrologueV2IncludeAndFileTable(P, DebugStrPool, |
| 1056 | DebugLineStrPool); |
| 1057 | else |
| 1058 | emitLineTablePrologueV5IncludeAndFileTable(P, DebugStrPool, |
| 1059 | DebugLineStrPool); |
| 1060 | } |
| 1061 | |
| 1062 | void DwarfStreamer::emitLineTableRows( |
| 1063 | const DWARFDebugLine::LineTable &LineTable, MCSymbol *LineEndSym, |
| 1064 | unsigned AddressByteSize, std::vector<uint64_t> *RowOffsets) { |
| 1065 | |
| 1066 | MCDwarfLineTableParams Params; |
| 1067 | Params.DWARF2LineOpcodeBase = LineTable.Prologue.OpcodeBase; |
| 1068 | Params.DWARF2LineBase = LineTable.Prologue.LineBase; |
| 1069 | Params.DWARF2LineRange = LineTable.Prologue.LineRange; |
| 1070 | |
| 1071 | SmallString<128> EncodingBuffer; |
| 1072 | |
| 1073 | if (LineTable.Rows.empty()) { |
| 1074 | // We only have the dummy entry, dsymutil emits an entry with a 0 |
| 1075 | // address in that case. |
| 1076 | MCDwarfLineAddr::encode(Context&: *MC, Params, LineDelta: std::numeric_limits<int64_t>::max(), AddrDelta: 0, |
| 1077 | OS&: EncodingBuffer); |
| 1078 | MS->emitBytes(Data: EncodingBuffer); |
| 1079 | LineSectionSize += EncodingBuffer.size(); |
| 1080 | MS->emitLabel(Symbol: LineEndSym); |
| 1081 | return; |
| 1082 | } |
| 1083 | |
| 1084 | // Line table state machine fields |
| 1085 | unsigned FileNum = 1; |
| 1086 | unsigned LastLine = 1; |
| 1087 | unsigned Column = 0; |
| 1088 | unsigned Discriminator = 0; |
| 1089 | unsigned IsStatement = 1; |
| 1090 | unsigned Isa = 0; |
| 1091 | uint64_t Address = -1ULL; |
| 1092 | |
| 1093 | unsigned RowsSinceLastSequence = 0; |
| 1094 | |
| 1095 | for (const DWARFDebugLine::Row &Row : LineTable.Rows) { |
| 1096 | // If we're tracking row offsets, record the current section size as the |
| 1097 | // offset of this row. |
| 1098 | if (RowOffsets) |
| 1099 | RowOffsets->push_back(x: LineSectionSize); |
| 1100 | |
| 1101 | int64_t AddressDelta; |
| 1102 | if (Address == -1ULL) { |
| 1103 | MS->emitIntValue(Value: dwarf::DW_LNS_extended_op, Size: 1); |
| 1104 | MS->emitULEB128IntValue(Value: AddressByteSize + 1); |
| 1105 | MS->emitIntValue(Value: dwarf::DW_LNE_set_address, Size: 1); |
| 1106 | MS->emitIntValue(Value: Row.Address.Address, Size: AddressByteSize); |
| 1107 | LineSectionSize += |
| 1108 | 2 + AddressByteSize + getULEB128Size(Value: AddressByteSize + 1); |
| 1109 | AddressDelta = 0; |
| 1110 | } else { |
| 1111 | AddressDelta = |
| 1112 | (Row.Address.Address - Address) / LineTable.Prologue.MinInstLength; |
| 1113 | } |
| 1114 | |
| 1115 | // FIXME: code copied and transformed from MCDwarf.cpp::EmitDwarfLineTable. |
| 1116 | // We should find a way to share this code, but the current compatibility |
| 1117 | // requirement with classic dsymutil makes it hard. Revisit that once this |
| 1118 | // requirement is dropped. |
| 1119 | |
| 1120 | if (FileNum != Row.File) { |
| 1121 | FileNum = Row.File; |
| 1122 | MS->emitIntValue(Value: dwarf::DW_LNS_set_file, Size: 1); |
| 1123 | MS->emitULEB128IntValue(Value: FileNum); |
| 1124 | LineSectionSize += 1 + getULEB128Size(Value: FileNum); |
| 1125 | } |
| 1126 | if (Column != Row.Column) { |
| 1127 | Column = Row.Column; |
| 1128 | MS->emitIntValue(Value: dwarf::DW_LNS_set_column, Size: 1); |
| 1129 | MS->emitULEB128IntValue(Value: Column); |
| 1130 | LineSectionSize += 1 + getULEB128Size(Value: Column); |
| 1131 | } |
| 1132 | if (Discriminator != Row.Discriminator && |
| 1133 | MS->getContext().getDwarfVersion() >= 4) { |
| 1134 | Discriminator = Row.Discriminator; |
| 1135 | unsigned Size = getULEB128Size(Value: Discriminator); |
| 1136 | MS->emitIntValue(Value: dwarf::DW_LNS_extended_op, Size: 1); |
| 1137 | MS->emitULEB128IntValue(Value: Size + 1); |
| 1138 | MS->emitIntValue(Value: dwarf::DW_LNE_set_discriminator, Size: 1); |
| 1139 | MS->emitULEB128IntValue(Value: Discriminator); |
| 1140 | LineSectionSize += /* extended op */ 1 + getULEB128Size(Value: Size + 1) + |
| 1141 | /* discriminator */ 1 + Size; |
| 1142 | } |
| 1143 | Discriminator = 0; |
| 1144 | |
| 1145 | if (Isa != Row.Isa) { |
| 1146 | Isa = Row.Isa; |
| 1147 | MS->emitIntValue(Value: dwarf::DW_LNS_set_isa, Size: 1); |
| 1148 | MS->emitULEB128IntValue(Value: Isa); |
| 1149 | LineSectionSize += 1 + getULEB128Size(Value: Isa); |
| 1150 | } |
| 1151 | if (IsStatement != Row.IsStmt) { |
| 1152 | IsStatement = Row.IsStmt; |
| 1153 | MS->emitIntValue(Value: dwarf::DW_LNS_negate_stmt, Size: 1); |
| 1154 | LineSectionSize += 1; |
| 1155 | } |
| 1156 | if (Row.BasicBlock) { |
| 1157 | MS->emitIntValue(Value: dwarf::DW_LNS_set_basic_block, Size: 1); |
| 1158 | LineSectionSize += 1; |
| 1159 | } |
| 1160 | |
| 1161 | if (Row.PrologueEnd) { |
| 1162 | MS->emitIntValue(Value: dwarf::DW_LNS_set_prologue_end, Size: 1); |
| 1163 | LineSectionSize += 1; |
| 1164 | } |
| 1165 | |
| 1166 | if (Row.EpilogueBegin) { |
| 1167 | MS->emitIntValue(Value: dwarf::DW_LNS_set_epilogue_begin, Size: 1); |
| 1168 | LineSectionSize += 1; |
| 1169 | } |
| 1170 | |
| 1171 | int64_t LineDelta = int64_t(Row.Line) - LastLine; |
| 1172 | if (!Row.EndSequence) { |
| 1173 | MCDwarfLineAddr::encode(Context&: *MC, Params, LineDelta, AddrDelta: AddressDelta, |
| 1174 | OS&: EncodingBuffer); |
| 1175 | MS->emitBytes(Data: EncodingBuffer); |
| 1176 | LineSectionSize += EncodingBuffer.size(); |
| 1177 | EncodingBuffer.resize(N: 0); |
| 1178 | Address = Row.Address.Address; |
| 1179 | LastLine = Row.Line; |
| 1180 | RowsSinceLastSequence++; |
| 1181 | } else { |
| 1182 | if (LineDelta) { |
| 1183 | MS->emitIntValue(Value: dwarf::DW_LNS_advance_line, Size: 1); |
| 1184 | MS->emitSLEB128IntValue(Value: LineDelta); |
| 1185 | LineSectionSize += 1 + getSLEB128Size(Value: LineDelta); |
| 1186 | } |
| 1187 | if (AddressDelta) { |
| 1188 | MS->emitIntValue(Value: dwarf::DW_LNS_advance_pc, Size: 1); |
| 1189 | MS->emitULEB128IntValue(Value: AddressDelta); |
| 1190 | LineSectionSize += 1 + getULEB128Size(Value: AddressDelta); |
| 1191 | } |
| 1192 | MCDwarfLineAddr::encode(Context&: *MC, Params, LineDelta: std::numeric_limits<int64_t>::max(), |
| 1193 | AddrDelta: 0, OS&: EncodingBuffer); |
| 1194 | MS->emitBytes(Data: EncodingBuffer); |
| 1195 | LineSectionSize += EncodingBuffer.size(); |
| 1196 | EncodingBuffer.resize(N: 0); |
| 1197 | Address = -1ULL; |
| 1198 | LastLine = FileNum = IsStatement = 1; |
| 1199 | RowsSinceLastSequence = Column = Discriminator = Isa = 0; |
| 1200 | } |
| 1201 | } |
| 1202 | |
| 1203 | if (RowsSinceLastSequence) { |
| 1204 | MCDwarfLineAddr::encode(Context&: *MC, Params, LineDelta: std::numeric_limits<int64_t>::max(), AddrDelta: 0, |
| 1205 | OS&: EncodingBuffer); |
| 1206 | MS->emitBytes(Data: EncodingBuffer); |
| 1207 | LineSectionSize += EncodingBuffer.size(); |
| 1208 | EncodingBuffer.resize(N: 0); |
| 1209 | } |
| 1210 | |
| 1211 | MS->emitLabel(Symbol: LineEndSym); |
| 1212 | } |
| 1213 | |
| 1214 | void DwarfStreamer::emitIntOffset(uint64_t Offset, dwarf::DwarfFormat Format, |
| 1215 | uint64_t &SectionSize) { |
| 1216 | uint8_t Size = dwarf::getDwarfOffsetByteSize(Format); |
| 1217 | MS->emitIntValue(Value: Offset, Size); |
| 1218 | SectionSize += Size; |
| 1219 | } |
| 1220 | |
| 1221 | void DwarfStreamer::emitLabelDifference(const MCSymbol *Hi, const MCSymbol *Lo, |
| 1222 | dwarf::DwarfFormat Format, |
| 1223 | uint64_t &SectionSize) { |
| 1224 | uint8_t Size = dwarf::getDwarfOffsetByteSize(Format); |
| 1225 | Asm->emitLabelDifference(Hi, Lo, Size); |
| 1226 | SectionSize += Size; |
| 1227 | } |
| 1228 | |
| 1229 | /// Emit the pubnames or pubtypes section contribution for \p |
| 1230 | /// Unit into \p Sec. The data is provided in \p Names. |
| 1231 | void DwarfStreamer::emitPubSectionForUnit( |
| 1232 | MCSection *Sec, StringRef SecName, const CompileUnit &Unit, |
| 1233 | const std::vector<CompileUnit::AccelInfo> &Names) { |
| 1234 | if (Names.empty()) |
| 1235 | return; |
| 1236 | |
| 1237 | // Start the dwarf pubnames section. |
| 1238 | Asm->OutStreamer->switchSection(Section: Sec); |
| 1239 | MCSymbol *BeginLabel = Asm->createTempSymbol(Name: "pub" + SecName + "_begin" ); |
| 1240 | MCSymbol *EndLabel = Asm->createTempSymbol(Name: "pub" + SecName + "_end" ); |
| 1241 | |
| 1242 | bool = false; |
| 1243 | // Emit the pubnames for this compilation unit. |
| 1244 | for (const auto &Name : Names) { |
| 1245 | if (Name.SkipPubSection) |
| 1246 | continue; |
| 1247 | |
| 1248 | if (!HeaderEmitted) { |
| 1249 | // Emit the header. |
| 1250 | Asm->emitLabelDifference(Hi: EndLabel, Lo: BeginLabel, Size: 4); // Length |
| 1251 | Asm->OutStreamer->emitLabel(Symbol: BeginLabel); |
| 1252 | Asm->emitInt16(Value: dwarf::DW_PUBNAMES_VERSION); // Version |
| 1253 | Asm->emitInt32(Value: Unit.getStartOffset()); // Unit offset |
| 1254 | Asm->emitInt32(Value: Unit.getNextUnitOffset() - Unit.getStartOffset()); // Size |
| 1255 | HeaderEmitted = true; |
| 1256 | } |
| 1257 | Asm->emitInt32(Value: Name.Die->getOffset()); |
| 1258 | |
| 1259 | // Emit the string itself. |
| 1260 | Asm->OutStreamer->emitBytes(Data: Name.Name.getString()); |
| 1261 | // Emit a null terminator. |
| 1262 | Asm->emitInt8(Value: 0); |
| 1263 | } |
| 1264 | |
| 1265 | if (!HeaderEmitted) |
| 1266 | return; |
| 1267 | Asm->emitInt32(Value: 0); // End marker. |
| 1268 | Asm->OutStreamer->emitLabel(Symbol: EndLabel); |
| 1269 | } |
| 1270 | |
| 1271 | /// Emit .debug_pubnames for \p Unit. |
| 1272 | void DwarfStreamer::emitPubNamesForUnit(const CompileUnit &Unit) { |
| 1273 | emitPubSectionForUnit(Sec: MC->getObjectFileInfo()->getDwarfPubNamesSection(), |
| 1274 | SecName: "names" , Unit, Names: Unit.getPubnames()); |
| 1275 | } |
| 1276 | |
| 1277 | /// Emit .debug_pubtypes for \p Unit. |
| 1278 | void DwarfStreamer::emitPubTypesForUnit(const CompileUnit &Unit) { |
| 1279 | emitPubSectionForUnit(Sec: MC->getObjectFileInfo()->getDwarfPubTypesSection(), |
| 1280 | SecName: "types" , Unit, Names: Unit.getPubtypes()); |
| 1281 | } |
| 1282 | |
| 1283 | /// Emit a CIE into the debug_frame section. |
| 1284 | void DwarfStreamer::emitCIE(StringRef CIEBytes) { |
| 1285 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfFrameSection()); |
| 1286 | |
| 1287 | MS->emitBytes(Data: CIEBytes); |
| 1288 | FrameSectionSize += CIEBytes.size(); |
| 1289 | } |
| 1290 | |
| 1291 | /// Emit a FDE into the debug_frame section. \p FDEBytes |
| 1292 | /// contains the FDE data without the length, CIE offset and address |
| 1293 | /// which will be replaced with the parameter values. |
| 1294 | void DwarfStreamer::emitFDE(uint32_t CIEOffset, uint32_t AddrSize, |
| 1295 | uint64_t Address, StringRef FDEBytes) { |
| 1296 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfFrameSection()); |
| 1297 | |
| 1298 | MS->emitIntValue(Value: FDEBytes.size() + 4 + AddrSize, Size: 4); |
| 1299 | MS->emitIntValue(Value: CIEOffset, Size: 4); |
| 1300 | MS->emitIntValue(Value: Address, Size: AddrSize); |
| 1301 | MS->emitBytes(Data: FDEBytes); |
| 1302 | FrameSectionSize += FDEBytes.size() + 8 + AddrSize; |
| 1303 | } |
| 1304 | |
| 1305 | void DwarfStreamer::emitMacroTables(DWARFContext *Context, |
| 1306 | const Offset2UnitMap &UnitMacroMap, |
| 1307 | OffsetsStringPool &StringPool) { |
| 1308 | assert(Context != nullptr && "Empty DWARF context" ); |
| 1309 | |
| 1310 | // Check for .debug_macinfo table. |
| 1311 | if (const DWARFDebugMacro *Table = Context->getDebugMacinfo()) { |
| 1312 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfMacinfoSection()); |
| 1313 | emitMacroTableImpl(MacroTable: Table, UnitMacroMap, StringPool, OutOffset&: MacInfoSectionSize); |
| 1314 | } |
| 1315 | |
| 1316 | // Check for .debug_macro table. |
| 1317 | if (const DWARFDebugMacro *Table = Context->getDebugMacro()) { |
| 1318 | MS->switchSection(Section: MC->getObjectFileInfo()->getDwarfMacroSection()); |
| 1319 | emitMacroTableImpl(MacroTable: Table, UnitMacroMap, StringPool, OutOffset&: MacroSectionSize); |
| 1320 | } |
| 1321 | } |
| 1322 | |
| 1323 | void DwarfStreamer::emitMacroTableImpl(const DWARFDebugMacro *MacroTable, |
| 1324 | const Offset2UnitMap &UnitMacroMap, |
| 1325 | OffsetsStringPool &StringPool, |
| 1326 | uint64_t &OutOffset) { |
| 1327 | bool DefAttributeIsReported = false; |
| 1328 | bool UndefAttributeIsReported = false; |
| 1329 | bool ImportAttributeIsReported = false; |
| 1330 | for (const DWARFDebugMacro::MacroList &List : MacroTable->MacroLists) { |
| 1331 | Offset2UnitMap::const_iterator UnitIt = UnitMacroMap.find(Val: List.Offset); |
| 1332 | if (UnitIt == UnitMacroMap.end()) { |
| 1333 | warn(Warning: formatv( |
| 1334 | Fmt: "couldn`t find compile unit for the macro table with offset = {0:x}" , |
| 1335 | Vals: List.Offset)); |
| 1336 | continue; |
| 1337 | } |
| 1338 | |
| 1339 | // Skip macro table if the unit was not cloned. |
| 1340 | DIE *OutputUnitDIE = UnitIt->second->getOutputUnitDIE(); |
| 1341 | if (OutputUnitDIE == nullptr) |
| 1342 | continue; |
| 1343 | |
| 1344 | // Update macro attribute of cloned compile unit with the proper offset to |
| 1345 | // the macro table. |
| 1346 | bool = false; |
| 1347 | for (auto &V : OutputUnitDIE->values()) { |
| 1348 | if (V.getAttribute() == dwarf::DW_AT_macro_info) { |
| 1349 | V = DIEValue(V.getAttribute(), V.getForm(), DIEInteger(OutOffset)); |
| 1350 | break; |
| 1351 | } else if (V.getAttribute() == dwarf::DW_AT_macros) { |
| 1352 | hasDWARFv5Header = true; |
| 1353 | V = DIEValue(V.getAttribute(), V.getForm(), DIEInteger(OutOffset)); |
| 1354 | break; |
| 1355 | } |
| 1356 | } |
| 1357 | |
| 1358 | // Write DWARFv5 header. |
| 1359 | if (hasDWARFv5Header) { |
| 1360 | // Write header version. |
| 1361 | MS->emitIntValue(Value: List.Header.Version, Size: sizeof(List.Header.Version)); |
| 1362 | OutOffset += sizeof(List.Header.Version); |
| 1363 | |
| 1364 | uint8_t Flags = List.Header.Flags; |
| 1365 | |
| 1366 | // Check for OPCODE_OPERANDS_TABLE. |
| 1367 | if (Flags & |
| 1368 | DWARFDebugMacro::HeaderFlagMask::MACRO_OPCODE_OPERANDS_TABLE) { |
| 1369 | Flags &= ~DWARFDebugMacro::HeaderFlagMask::MACRO_OPCODE_OPERANDS_TABLE; |
| 1370 | warn(Warning: "opcode_operands_table is not supported yet." ); |
| 1371 | } |
| 1372 | |
| 1373 | // Check for DEBUG_LINE_OFFSET. |
| 1374 | std::optional<uint64_t> StmtListOffset; |
| 1375 | if (Flags & DWARFDebugMacro::HeaderFlagMask::MACRO_DEBUG_LINE_OFFSET) { |
| 1376 | // Get offset to the line table from the cloned compile unit. |
| 1377 | for (auto &V : OutputUnitDIE->values()) { |
| 1378 | if (V.getAttribute() == dwarf::DW_AT_stmt_list) { |
| 1379 | StmtListOffset = V.getDIEInteger().getValue(); |
| 1380 | break; |
| 1381 | } |
| 1382 | } |
| 1383 | |
| 1384 | if (!StmtListOffset) { |
| 1385 | Flags &= ~DWARFDebugMacro::HeaderFlagMask::MACRO_DEBUG_LINE_OFFSET; |
| 1386 | warn(Warning: "couldn`t find line table for macro table." ); |
| 1387 | } |
| 1388 | } |
| 1389 | |
| 1390 | // Write flags. |
| 1391 | MS->emitIntValue(Value: Flags, Size: sizeof(Flags)); |
| 1392 | OutOffset += sizeof(Flags); |
| 1393 | |
| 1394 | // Write offset to line table. |
| 1395 | if (StmtListOffset) { |
| 1396 | MS->emitIntValue(Value: *StmtListOffset, Size: List.Header.getOffsetByteSize()); |
| 1397 | OutOffset += List.Header.getOffsetByteSize(); |
| 1398 | } |
| 1399 | } |
| 1400 | |
| 1401 | // Write macro entries. |
| 1402 | for (const DWARFDebugMacro::Entry &MacroEntry : List.Macros) { |
| 1403 | if (MacroEntry.Type == 0) { |
| 1404 | OutOffset += MS->emitULEB128IntValue(Value: MacroEntry.Type); |
| 1405 | continue; |
| 1406 | } |
| 1407 | |
| 1408 | uint8_t MacroType = MacroEntry.Type; |
| 1409 | switch (MacroType) { |
| 1410 | default: { |
| 1411 | bool HasVendorSpecificExtension = |
| 1412 | (!hasDWARFv5Header && MacroType == dwarf::DW_MACINFO_vendor_ext) || |
| 1413 | (hasDWARFv5Header && (MacroType >= dwarf::DW_MACRO_lo_user && |
| 1414 | MacroType <= dwarf::DW_MACRO_hi_user)); |
| 1415 | |
| 1416 | if (HasVendorSpecificExtension) { |
| 1417 | // Write macinfo type. |
| 1418 | MS->emitIntValue(Value: MacroType, Size: 1); |
| 1419 | OutOffset++; |
| 1420 | |
| 1421 | // Write vendor extension constant. |
| 1422 | OutOffset += MS->emitULEB128IntValue(Value: MacroEntry.ExtConstant); |
| 1423 | |
| 1424 | // Write vendor extension string. |
| 1425 | StringRef String = MacroEntry.ExtStr; |
| 1426 | MS->emitBytes(Data: String); |
| 1427 | MS->emitIntValue(Value: 0, Size: 1); |
| 1428 | OutOffset += String.size() + 1; |
| 1429 | } else |
| 1430 | warn(Warning: "unknown macro type. skip." ); |
| 1431 | } break; |
| 1432 | // debug_macro and debug_macinfo share some common encodings. |
| 1433 | // DW_MACRO_define == DW_MACINFO_define |
| 1434 | // DW_MACRO_undef == DW_MACINFO_undef |
| 1435 | // DW_MACRO_start_file == DW_MACINFO_start_file |
| 1436 | // DW_MACRO_end_file == DW_MACINFO_end_file |
| 1437 | // For readibility/uniformity we are using DW_MACRO_*. |
| 1438 | case dwarf::DW_MACRO_define: |
| 1439 | case dwarf::DW_MACRO_undef: { |
| 1440 | // Write macinfo type. |
| 1441 | MS->emitIntValue(Value: MacroType, Size: 1); |
| 1442 | OutOffset++; |
| 1443 | |
| 1444 | // Write source line. |
| 1445 | OutOffset += MS->emitULEB128IntValue(Value: MacroEntry.Line); |
| 1446 | |
| 1447 | // Write macro string. |
| 1448 | StringRef String = MacroEntry.MacroStr; |
| 1449 | MS->emitBytes(Data: String); |
| 1450 | MS->emitIntValue(Value: 0, Size: 1); |
| 1451 | OutOffset += String.size() + 1; |
| 1452 | } break; |
| 1453 | case dwarf::DW_MACRO_define_strp: |
| 1454 | case dwarf::DW_MACRO_undef_strp: |
| 1455 | case dwarf::DW_MACRO_define_strx: |
| 1456 | case dwarf::DW_MACRO_undef_strx: { |
| 1457 | assert(UnitIt->second->getOrigUnit().getVersion() >= 5); |
| 1458 | |
| 1459 | // DW_MACRO_*_strx forms are not supported currently. |
| 1460 | // Convert to *_strp. |
| 1461 | switch (MacroType) { |
| 1462 | case dwarf::DW_MACRO_define_strx: { |
| 1463 | MacroType = dwarf::DW_MACRO_define_strp; |
| 1464 | if (!DefAttributeIsReported) { |
| 1465 | warn(Warning: "DW_MACRO_define_strx unsupported yet. Convert to " |
| 1466 | "DW_MACRO_define_strp." ); |
| 1467 | DefAttributeIsReported = true; |
| 1468 | } |
| 1469 | } break; |
| 1470 | case dwarf::DW_MACRO_undef_strx: { |
| 1471 | MacroType = dwarf::DW_MACRO_undef_strp; |
| 1472 | if (!UndefAttributeIsReported) { |
| 1473 | warn(Warning: "DW_MACRO_undef_strx unsupported yet. Convert to " |
| 1474 | "DW_MACRO_undef_strp." ); |
| 1475 | UndefAttributeIsReported = true; |
| 1476 | } |
| 1477 | } break; |
| 1478 | default: |
| 1479 | // Nothing to do. |
| 1480 | break; |
| 1481 | } |
| 1482 | |
| 1483 | // Write macinfo type. |
| 1484 | MS->emitIntValue(Value: MacroType, Size: 1); |
| 1485 | OutOffset++; |
| 1486 | |
| 1487 | // Write source line. |
| 1488 | OutOffset += MS->emitULEB128IntValue(Value: MacroEntry.Line); |
| 1489 | |
| 1490 | // Write macro string. |
| 1491 | DwarfStringPoolEntryRef EntryRef = |
| 1492 | StringPool.getEntry(S: MacroEntry.MacroStr); |
| 1493 | MS->emitIntValue(Value: EntryRef.getOffset(), Size: List.Header.getOffsetByteSize()); |
| 1494 | OutOffset += List.Header.getOffsetByteSize(); |
| 1495 | break; |
| 1496 | } |
| 1497 | case dwarf::DW_MACRO_start_file: { |
| 1498 | // Write macinfo type. |
| 1499 | MS->emitIntValue(Value: MacroType, Size: 1); |
| 1500 | OutOffset++; |
| 1501 | // Write source line. |
| 1502 | OutOffset += MS->emitULEB128IntValue(Value: MacroEntry.Line); |
| 1503 | // Write source file id. |
| 1504 | OutOffset += MS->emitULEB128IntValue(Value: MacroEntry.File); |
| 1505 | } break; |
| 1506 | case dwarf::DW_MACRO_end_file: { |
| 1507 | // Write macinfo type. |
| 1508 | MS->emitIntValue(Value: MacroType, Size: 1); |
| 1509 | OutOffset++; |
| 1510 | } break; |
| 1511 | case dwarf::DW_MACRO_import: |
| 1512 | case dwarf::DW_MACRO_import_sup: { |
| 1513 | if (!ImportAttributeIsReported) { |
| 1514 | warn(Warning: "DW_MACRO_import and DW_MACRO_import_sup are unsupported yet. " |
| 1515 | "remove." ); |
| 1516 | ImportAttributeIsReported = true; |
| 1517 | } |
| 1518 | } break; |
| 1519 | } |
| 1520 | } |
| 1521 | } |
| 1522 | } |
| 1523 | |