1 //===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===// 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 // A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF 10 // package files). 11 // 12 //===----------------------------------------------------------------------===// 13 #include "DWPError.h" 14 #include "DWPStringPool.h" 15 #include "llvm/ADT/MapVector.h" 16 #include "llvm/ADT/Optional.h" 17 #include "llvm/ADT/STLExtras.h" 18 #include "llvm/DebugInfo/DWARF/DWARFContext.h" 19 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 20 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" 21 #include "llvm/MC/MCAsmBackend.h" 22 #include "llvm/MC/MCAsmInfo.h" 23 #include "llvm/MC/MCCodeEmitter.h" 24 #include "llvm/MC/MCContext.h" 25 #include "llvm/MC/MCInstrInfo.h" 26 #include "llvm/MC/MCObjectFileInfo.h" 27 #include "llvm/MC/MCObjectWriter.h" 28 #include "llvm/MC/MCRegisterInfo.h" 29 #include "llvm/MC/MCStreamer.h" 30 #include "llvm/MC/MCTargetOptionsCommandFlags.h" 31 #include "llvm/Object/Decompressor.h" 32 #include "llvm/Object/ObjectFile.h" 33 #include "llvm/Support/CommandLine.h" 34 #include "llvm/Support/DataExtractor.h" 35 #include "llvm/Support/Error.h" 36 #include "llvm/Support/FileSystem.h" 37 #include "llvm/Support/InitLLVM.h" 38 #include "llvm/Support/MathExtras.h" 39 #include "llvm/Support/MemoryBuffer.h" 40 #include "llvm/Support/Path.h" 41 #include "llvm/Support/TargetRegistry.h" 42 #include "llvm/Support/TargetSelect.h" 43 #include "llvm/Support/ToolOutputFile.h" 44 #include "llvm/Support/WithColor.h" 45 #include "llvm/Support/raw_ostream.h" 46 47 using namespace llvm; 48 using namespace llvm::object; 49 50 static mc::RegisterMCTargetOptionsFlags MCTargetOptionsFlags; 51 52 cl::OptionCategory DwpCategory("Specific Options"); 53 static cl::list<std::string> InputFiles(cl::Positional, cl::ZeroOrMore, 54 cl::desc("<input files>"), 55 cl::cat(DwpCategory)); 56 57 static cl::list<std::string> ExecFilenames( 58 "e", cl::ZeroOrMore, 59 cl::desc("Specify the executable/library files to get the list of *.dwo from"), 60 cl::value_desc("filename"), cl::cat(DwpCategory)); 61 62 static cl::opt<std::string> OutputFilename(cl::Required, "o", 63 cl::desc("Specify the output file."), 64 cl::value_desc("filename"), 65 cl::cat(DwpCategory)); 66 67 static void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings, 68 MCSection *StrOffsetSection, 69 StringRef CurStrSection, 70 StringRef CurStrOffsetSection) { 71 // Could possibly produce an error or warning if one of these was non-null but 72 // the other was null. 73 if (CurStrSection.empty() || CurStrOffsetSection.empty()) 74 return; 75 76 DenseMap<uint64_t, uint32_t> OffsetRemapping; 77 78 DataExtractor Data(CurStrSection, true, 0); 79 uint64_t LocalOffset = 0; 80 uint64_t PrevOffset = 0; 81 while (const char *s = Data.getCStr(&LocalOffset)) { 82 OffsetRemapping[PrevOffset] = 83 Strings.getOffset(s, LocalOffset - PrevOffset); 84 PrevOffset = LocalOffset; 85 } 86 87 Data = DataExtractor(CurStrOffsetSection, true, 0); 88 89 Out.SwitchSection(StrOffsetSection); 90 91 uint64_t Offset = 0; 92 uint64_t Size = CurStrOffsetSection.size(); 93 while (Offset < Size) { 94 auto OldOffset = Data.getU32(&Offset); 95 auto NewOffset = OffsetRemapping[OldOffset]; 96 Out.emitIntValue(NewOffset, 4); 97 } 98 } 99 100 static uint64_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) { 101 uint64_t CurCode; 102 uint64_t Offset = 0; 103 DataExtractor AbbrevData(Abbrev, true, 0); 104 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) { 105 // Tag 106 AbbrevData.getULEB128(&Offset); 107 // DW_CHILDREN 108 AbbrevData.getU8(&Offset); 109 // Attributes 110 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset)) 111 ; 112 } 113 return Offset; 114 } 115 116 struct CompileUnitIdentifiers { 117 uint64_t Signature = 0; 118 const char *Name = ""; 119 const char *DWOName = ""; 120 }; 121 122 static Expected<const char *> 123 getIndexedString(dwarf::Form Form, DataExtractor InfoData, 124 uint64_t &InfoOffset, StringRef StrOffsets, StringRef Str) { 125 if (Form == dwarf::DW_FORM_string) 126 return InfoData.getCStr(&InfoOffset); 127 if (Form != dwarf::DW_FORM_GNU_str_index) 128 return make_error<DWPError>( 129 "string field encoded without DW_FORM_string or DW_FORM_GNU_str_index"); 130 auto StrIndex = InfoData.getULEB128(&InfoOffset); 131 DataExtractor StrOffsetsData(StrOffsets, true, 0); 132 uint64_t StrOffsetsOffset = 4 * StrIndex; 133 uint64_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset); 134 DataExtractor StrData(Str, true, 0); 135 return StrData.getCStr(&StrOffset); 136 } 137 138 static Expected<CompileUnitIdentifiers> getCUIdentifiers(StringRef Abbrev, 139 StringRef Info, 140 StringRef StrOffsets, 141 StringRef Str) { 142 uint64_t Offset = 0; 143 DataExtractor InfoData(Info, true, 0); 144 dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32; 145 uint64_t Length = InfoData.getU32(&Offset); 146 CompileUnitIdentifiers ID; 147 Optional<uint64_t> Signature = None; 148 // If the length is 0xffffffff, then this indictes that this is a DWARF 64 149 // stream and the length is actually encoded into a 64 bit value that follows. 150 if (Length == 0xffffffffU) { 151 Format = dwarf::DwarfFormat::DWARF64; 152 Length = InfoData.getU64(&Offset); 153 } 154 uint16_t Version = InfoData.getU16(&Offset); 155 if (Version >= 5) { 156 auto UnitType = InfoData.getU8(&Offset); 157 if (UnitType != dwarf::DW_UT_split_compile) 158 return make_error<DWPError>( 159 std::string("unit type DW_UT_split_compile type not found in " 160 "debug_info header. Unexpected unit type 0x" + 161 utostr(UnitType) + " found")); 162 } 163 InfoData.getU32(&Offset); // Abbrev offset (should be zero) 164 uint8_t AddrSize = InfoData.getU8(&Offset); 165 if (Version >= 5) 166 Signature = InfoData.getU64(&Offset); 167 uint32_t AbbrCode = InfoData.getULEB128(&Offset); 168 169 DataExtractor AbbrevData(Abbrev, true, 0); 170 uint64_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode); 171 auto Tag = static_cast<dwarf::Tag>(AbbrevData.getULEB128(&AbbrevOffset)); 172 if (Tag != dwarf::DW_TAG_compile_unit) 173 return make_error<DWPError>("top level DIE is not a compile unit"); 174 // DW_CHILDREN 175 AbbrevData.getU8(&AbbrevOffset); 176 uint32_t Name; 177 dwarf::Form Form; 178 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) | 179 (Form = static_cast<dwarf::Form>(AbbrevData.getULEB128(&AbbrevOffset))) && 180 (Name != 0 || Form != 0)) { 181 switch (Name) { 182 case dwarf::DW_AT_name: { 183 Expected<const char *> EName = 184 getIndexedString(Form, InfoData, Offset, StrOffsets, Str); 185 if (!EName) 186 return EName.takeError(); 187 ID.Name = *EName; 188 break; 189 } 190 case dwarf::DW_AT_GNU_dwo_name: 191 case dwarf::DW_AT_dwo_name: { 192 Expected<const char *> EName = 193 getIndexedString(Form, InfoData, Offset, StrOffsets, Str); 194 if (!EName) 195 return EName.takeError(); 196 ID.DWOName = *EName; 197 break; 198 } 199 case dwarf::DW_AT_GNU_dwo_id: 200 Signature = InfoData.getU64(&Offset); 201 break; 202 default: 203 DWARFFormValue::skipValue(Form, InfoData, &Offset, 204 dwarf::FormParams({Version, AddrSize, Format})); 205 } 206 } 207 if (!Signature) 208 return make_error<DWPError>("compile unit missing dwo_id"); 209 ID.Signature = *Signature; 210 return ID; 211 } 212 213 struct UnitIndexEntry { 214 DWARFUnitIndex::Entry::SectionContribution Contributions[8]; 215 std::string Name; 216 std::string DWOName; 217 StringRef DWPName; 218 }; 219 220 static bool isSupportedSectionKind(DWARFSectionKind Kind) { 221 return Kind != DW_SECT_EXT_unknown; 222 } 223 224 // Convert an internal section identifier into the index to use with 225 // UnitIndexEntry::Contributions. 226 static unsigned getContributionIndex(DWARFSectionKind Kind) { 227 // Assuming the pre-standard DWP format. 228 assert(serializeSectionKind(Kind, 2) >= DW_SECT_INFO); 229 return serializeSectionKind(Kind, 2) - DW_SECT_INFO; 230 } 231 232 // Convert a UnitIndexEntry::Contributions index to the corresponding on-disk 233 // value of the section identifier. 234 static unsigned getOnDiskSectionId(unsigned Index) { 235 return Index + DW_SECT_INFO; 236 } 237 238 static StringRef getSubsection(StringRef Section, 239 const DWARFUnitIndex::Entry &Entry, 240 DWARFSectionKind Kind) { 241 const auto *Off = Entry.getContribution(Kind); 242 if (!Off) 243 return StringRef(); 244 return Section.substr(Off->Offset, Off->Length); 245 } 246 247 static void addAllTypesFromDWP( 248 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries, 249 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types, 250 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) { 251 Out.SwitchSection(OutputTypes); 252 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) { 253 auto *I = E.getContributions(); 254 if (!I) 255 continue; 256 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry)); 257 if (!P.second) 258 continue; 259 auto &Entry = P.first->second; 260 // Zero out the debug_info contribution 261 Entry.Contributions[0] = {}; 262 for (auto Kind : TUIndex.getColumnKinds()) { 263 if (!isSupportedSectionKind(Kind)) 264 continue; 265 auto &C = Entry.Contributions[getContributionIndex(Kind)]; 266 C.Offset += I->Offset; 267 C.Length = I->Length; 268 ++I; 269 } 270 unsigned TypesIndex = getContributionIndex(DW_SECT_EXT_TYPES); 271 auto &C = Entry.Contributions[TypesIndex]; 272 Out.emitBytes(Types.substr( 273 C.Offset - TUEntry.Contributions[TypesIndex].Offset, C.Length)); 274 C.Offset = TypesOffset; 275 TypesOffset += C.Length; 276 } 277 } 278 279 static void addAllTypes(MCStreamer &Out, 280 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries, 281 MCSection *OutputTypes, 282 const std::vector<StringRef> &TypesSections, 283 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) { 284 for (StringRef Types : TypesSections) { 285 Out.SwitchSection(OutputTypes); 286 uint64_t Offset = 0; 287 DataExtractor Data(Types, true, 0); 288 while (Data.isValidOffset(Offset)) { 289 UnitIndexEntry Entry = CUEntry; 290 // Zero out the debug_info contribution 291 Entry.Contributions[0] = {}; 292 auto &C = Entry.Contributions[getContributionIndex(DW_SECT_EXT_TYPES)]; 293 C.Offset = TypesOffset; 294 auto PrevOffset = Offset; 295 // Length of the unit, including the 4 byte length field. 296 C.Length = Data.getU32(&Offset) + 4; 297 298 Data.getU16(&Offset); // Version 299 Data.getU32(&Offset); // Abbrev offset 300 Data.getU8(&Offset); // Address size 301 auto Signature = Data.getU64(&Offset); 302 Offset = PrevOffset + C.Length; 303 304 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry)); 305 if (!P.second) 306 continue; 307 308 Out.emitBytes(Types.substr(PrevOffset, C.Length)); 309 TypesOffset += C.Length; 310 } 311 } 312 } 313 314 static void 315 writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets, 316 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries, 317 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) { 318 for (const auto &E : IndexEntries) 319 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i) 320 if (ContributionOffsets[i]) 321 Out.emitIntValue(E.second.Contributions[i].*Field, 4); 322 } 323 324 static void 325 writeIndex(MCStreamer &Out, MCSection *Section, 326 ArrayRef<unsigned> ContributionOffsets, 327 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) { 328 if (IndexEntries.empty()) 329 return; 330 331 unsigned Columns = 0; 332 for (auto &C : ContributionOffsets) 333 if (C) 334 ++Columns; 335 336 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2)); 337 uint64_t Mask = Buckets.size() - 1; 338 size_t i = 0; 339 for (const auto &P : IndexEntries) { 340 auto S = P.first; 341 auto H = S & Mask; 342 auto HP = ((S >> 32) & Mask) | 1; 343 while (Buckets[H]) { 344 assert(S != IndexEntries.begin()[Buckets[H] - 1].first && 345 "Duplicate unit"); 346 H = (H + HP) & Mask; 347 } 348 Buckets[H] = i + 1; 349 ++i; 350 } 351 352 Out.SwitchSection(Section); 353 Out.emitIntValue(2, 4); // Version 354 Out.emitIntValue(Columns, 4); // Columns 355 Out.emitIntValue(IndexEntries.size(), 4); // Num Units 356 Out.emitIntValue(Buckets.size(), 4); // Num Buckets 357 358 // Write the signatures. 359 for (const auto &I : Buckets) 360 Out.emitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8); 361 362 // Write the indexes. 363 for (const auto &I : Buckets) 364 Out.emitIntValue(I, 4); 365 366 // Write the column headers (which sections will appear in the table) 367 for (size_t i = 0; i != ContributionOffsets.size(); ++i) 368 if (ContributionOffsets[i]) 369 Out.emitIntValue(getOnDiskSectionId(i), 4); 370 371 // Write the offsets. 372 writeIndexTable(Out, ContributionOffsets, IndexEntries, 373 &DWARFUnitIndex::Entry::SectionContribution::Offset); 374 375 // Write the lengths. 376 writeIndexTable(Out, ContributionOffsets, IndexEntries, 377 &DWARFUnitIndex::Entry::SectionContribution::Length); 378 } 379 380 static std::string buildDWODescription(StringRef Name, StringRef DWPName, 381 StringRef DWOName) { 382 std::string Text = "\'"; 383 Text += Name; 384 Text += '\''; 385 if (!DWPName.empty()) { 386 Text += " (from "; 387 if (!DWOName.empty()) { 388 Text += '\''; 389 Text += DWOName; 390 Text += "' in "; 391 } 392 Text += '\''; 393 Text += DWPName; 394 Text += "')"; 395 } 396 return Text; 397 } 398 399 static Error createError(StringRef Name, Error E) { 400 return make_error<DWPError>( 401 ("failure while decompressing compressed section: '" + Name + "', " + 402 llvm::toString(std::move(E))) 403 .str()); 404 } 405 406 static Error 407 handleCompressedSection(std::deque<SmallString<32>> &UncompressedSections, 408 StringRef &Name, StringRef &Contents) { 409 if (!Decompressor::isGnuStyle(Name)) 410 return Error::success(); 411 412 Expected<Decompressor> Dec = 413 Decompressor::create(Name, Contents, false /*IsLE*/, false /*Is64Bit*/); 414 if (!Dec) 415 return createError(Name, Dec.takeError()); 416 417 UncompressedSections.emplace_back(); 418 if (Error E = Dec->resizeAndDecompress(UncompressedSections.back())) 419 return createError(Name, std::move(E)); 420 421 Name = Name.substr(2); // Drop ".z" 422 Contents = UncompressedSections.back(); 423 return Error::success(); 424 } 425 426 static Error handleSection( 427 const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections, 428 const MCSection *StrSection, const MCSection *StrOffsetSection, 429 const MCSection *TypesSection, const MCSection *CUIndexSection, 430 const MCSection *TUIndexSection, const SectionRef &Section, MCStreamer &Out, 431 std::deque<SmallString<32>> &UncompressedSections, 432 uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry, 433 StringRef &CurStrSection, StringRef &CurStrOffsetSection, 434 std::vector<StringRef> &CurTypesSection, StringRef &InfoSection, 435 StringRef &AbbrevSection, StringRef &CurCUIndexSection, 436 StringRef &CurTUIndexSection) { 437 if (Section.isBSS()) 438 return Error::success(); 439 440 if (Section.isVirtual()) 441 return Error::success(); 442 443 Expected<StringRef> NameOrErr = Section.getName(); 444 if (!NameOrErr) 445 return NameOrErr.takeError(); 446 StringRef Name = *NameOrErr; 447 448 Expected<StringRef> ContentsOrErr = Section.getContents(); 449 if (!ContentsOrErr) 450 return ContentsOrErr.takeError(); 451 StringRef Contents = *ContentsOrErr; 452 453 if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents)) 454 return Err; 455 456 Name = Name.substr(Name.find_first_not_of("._")); 457 458 auto SectionPair = KnownSections.find(Name); 459 if (SectionPair == KnownSections.end()) 460 return Error::success(); 461 462 if (DWARFSectionKind Kind = SectionPair->second.second) { 463 auto Index = getContributionIndex(Kind); 464 if (Kind != DW_SECT_EXT_TYPES) { 465 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index]; 466 ContributionOffsets[Index] += 467 (CurEntry.Contributions[Index].Length = Contents.size()); 468 } 469 470 switch (Kind) { 471 case DW_SECT_INFO: 472 InfoSection = Contents; 473 break; 474 case DW_SECT_ABBREV: 475 AbbrevSection = Contents; 476 break; 477 default: 478 break; 479 } 480 } 481 482 MCSection *OutSection = SectionPair->second.first; 483 if (OutSection == StrOffsetSection) 484 CurStrOffsetSection = Contents; 485 else if (OutSection == StrSection) 486 CurStrSection = Contents; 487 else if (OutSection == TypesSection) 488 CurTypesSection.push_back(Contents); 489 else if (OutSection == CUIndexSection) 490 CurCUIndexSection = Contents; 491 else if (OutSection == TUIndexSection) 492 CurTUIndexSection = Contents; 493 else { 494 Out.SwitchSection(OutSection); 495 Out.emitBytes(Contents); 496 } 497 return Error::success(); 498 } 499 500 static Error 501 buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE, 502 const CompileUnitIdentifiers &ID, StringRef DWPName) { 503 return make_error<DWPError>( 504 std::string("duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " + 505 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName, 506 PrevE.second.DWOName) + 507 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName)); 508 } 509 510 static Expected<SmallVector<std::string, 16>> 511 getDWOFilenames(StringRef ExecFilename) { 512 auto ErrOrObj = object::ObjectFile::createObjectFile(ExecFilename); 513 if (!ErrOrObj) 514 return ErrOrObj.takeError(); 515 516 const ObjectFile &Obj = *ErrOrObj.get().getBinary(); 517 std::unique_ptr<DWARFContext> DWARFCtx = DWARFContext::create(Obj); 518 519 SmallVector<std::string, 16> DWOPaths; 520 for (const auto &CU : DWARFCtx->compile_units()) { 521 const DWARFDie &Die = CU->getUnitDIE(); 522 std::string DWOName = dwarf::toString( 523 Die.find({dwarf::DW_AT_dwo_name, dwarf::DW_AT_GNU_dwo_name}), ""); 524 if (DWOName.empty()) 525 continue; 526 std::string DWOCompDir = 527 dwarf::toString(Die.find(dwarf::DW_AT_comp_dir), ""); 528 if (!DWOCompDir.empty()) { 529 SmallString<16> DWOPath(std::move(DWOName)); 530 sys::fs::make_absolute(DWOCompDir, DWOPath); 531 DWOPaths.emplace_back(DWOPath.data(), DWOPath.size()); 532 } else { 533 DWOPaths.push_back(std::move(DWOName)); 534 } 535 } 536 return std::move(DWOPaths); 537 } 538 539 static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) { 540 const auto &MCOFI = *Out.getContext().getObjectFileInfo(); 541 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection(); 542 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection(); 543 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection(); 544 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection(); 545 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection(); 546 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = { 547 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}}, 548 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_EXT_TYPES}}, 549 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}}, 550 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}}, 551 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_EXT_LOC}}, 552 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}}, 553 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}, 554 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}}, 555 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}}; 556 557 MapVector<uint64_t, UnitIndexEntry> IndexEntries; 558 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries; 559 560 uint32_t ContributionOffsets[8] = {}; 561 562 DWPStringPool Strings(Out, StrSection); 563 564 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects; 565 Objects.reserve(Inputs.size()); 566 567 std::deque<SmallString<32>> UncompressedSections; 568 569 for (const auto &Input : Inputs) { 570 auto ErrOrObj = object::ObjectFile::createObjectFile(Input); 571 if (!ErrOrObj) 572 return ErrOrObj.takeError(); 573 574 auto &Obj = *ErrOrObj->getBinary(); 575 Objects.push_back(std::move(*ErrOrObj)); 576 577 UnitIndexEntry CurEntry = {}; 578 579 StringRef CurStrSection; 580 StringRef CurStrOffsetSection; 581 std::vector<StringRef> CurTypesSection; 582 StringRef InfoSection; 583 StringRef AbbrevSection; 584 StringRef CurCUIndexSection; 585 StringRef CurTUIndexSection; 586 587 for (const auto &Section : Obj.sections()) 588 if (auto Err = handleSection( 589 KnownSections, StrSection, StrOffsetSection, TypesSection, 590 CUIndexSection, TUIndexSection, Section, Out, 591 UncompressedSections, ContributionOffsets, CurEntry, 592 CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection, 593 AbbrevSection, CurCUIndexSection, CurTUIndexSection)) 594 return Err; 595 596 if (InfoSection.empty()) 597 continue; 598 599 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection, 600 CurStrOffsetSection); 601 602 if (CurCUIndexSection.empty()) { 603 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers( 604 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection); 605 if (!EID) 606 return createFileError(Input, EID.takeError()); 607 const auto &ID = *EID; 608 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry)); 609 if (!P.second) 610 return buildDuplicateError(*P.first, ID, ""); 611 P.first->second.Name = ID.Name; 612 P.first->second.DWOName = ID.DWOName; 613 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, 614 CurEntry, 615 ContributionOffsets[getContributionIndex(DW_SECT_EXT_TYPES)]); 616 continue; 617 } 618 619 DWARFUnitIndex CUIndex(DW_SECT_INFO); 620 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0); 621 if (!CUIndex.parse(CUIndexData)) 622 return make_error<DWPError>("failed to parse cu_index"); 623 if (CUIndex.getVersion() != 2) 624 return make_error<DWPError>( 625 "unsupported cu_index version: " + utostr(CUIndex.getVersion()) + 626 " (only version 2 is supported)"); 627 628 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) { 629 auto *I = E.getContributions(); 630 if (!I) 631 continue; 632 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry)); 633 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers( 634 getSubsection(AbbrevSection, E, DW_SECT_ABBREV), 635 getSubsection(InfoSection, E, DW_SECT_INFO), 636 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS), 637 CurStrSection); 638 if (!EID) 639 return createFileError(Input, EID.takeError()); 640 const auto &ID = *EID; 641 if (!P.second) 642 return buildDuplicateError(*P.first, ID, Input); 643 auto &NewEntry = P.first->second; 644 NewEntry.Name = ID.Name; 645 NewEntry.DWOName = ID.DWOName; 646 NewEntry.DWPName = Input; 647 for (auto Kind : CUIndex.getColumnKinds()) { 648 if (!isSupportedSectionKind(Kind)) 649 continue; 650 auto &C = NewEntry.Contributions[getContributionIndex(Kind)]; 651 C.Offset += I->Offset; 652 C.Length = I->Length; 653 ++I; 654 } 655 } 656 657 if (!CurTypesSection.empty()) { 658 if (CurTypesSection.size() != 1) 659 return make_error<DWPError>("multiple type unit sections in .dwp file"); 660 DWARFUnitIndex TUIndex(DW_SECT_EXT_TYPES); 661 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0); 662 if (!TUIndex.parse(TUIndexData)) 663 return make_error<DWPError>("failed to parse tu_index"); 664 if (TUIndex.getVersion() != 2) 665 return make_error<DWPError>( 666 "unsupported tu_index version: " + utostr(TUIndex.getVersion()) + 667 " (only version 2 is supported)"); 668 669 addAllTypesFromDWP( 670 Out, TypeIndexEntries, TUIndex, TypesSection, CurTypesSection.front(), 671 CurEntry, 672 ContributionOffsets[getContributionIndex(DW_SECT_EXT_TYPES)]); 673 } 674 } 675 676 // Lie about there being no info contributions so the TU index only includes 677 // the type unit contribution 678 ContributionOffsets[0] = 0; 679 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets, 680 TypeIndexEntries); 681 682 // Lie about the type contribution 683 ContributionOffsets[getContributionIndex(DW_SECT_EXT_TYPES)] = 0; 684 // Unlie about the info contribution 685 ContributionOffsets[0] = 1; 686 687 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets, 688 IndexEntries); 689 690 return Error::success(); 691 } 692 693 static int error(const Twine &Error, const Twine &Context) { 694 errs() << Twine("while processing ") + Context + ":\n"; 695 errs() << Twine("error: ") + Error + "\n"; 696 return 1; 697 } 698 699 static Expected<Triple> readTargetTriple(StringRef FileName) { 700 auto ErrOrObj = object::ObjectFile::createObjectFile(FileName); 701 if (!ErrOrObj) 702 return ErrOrObj.takeError(); 703 704 return ErrOrObj->getBinary()->makeTriple(); 705 } 706 707 int main(int argc, char **argv) { 708 InitLLVM X(argc, argv); 709 710 cl::ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files\n"); 711 712 llvm::InitializeAllTargetInfos(); 713 llvm::InitializeAllTargetMCs(); 714 llvm::InitializeAllTargets(); 715 llvm::InitializeAllAsmPrinters(); 716 717 std::vector<std::string> DWOFilenames = InputFiles; 718 for (const auto &ExecFilename : ExecFilenames) { 719 auto DWOs = getDWOFilenames(ExecFilename); 720 if (!DWOs) { 721 logAllUnhandledErrors(DWOs.takeError(), WithColor::error()); 722 return 1; 723 } 724 DWOFilenames.insert(DWOFilenames.end(), 725 std::make_move_iterator(DWOs->begin()), 726 std::make_move_iterator(DWOs->end())); 727 } 728 729 if (DWOFilenames.empty()) 730 return 0; 731 732 std::string ErrorStr; 733 StringRef Context = "dwarf streamer init"; 734 735 auto ErrOrTriple = readTargetTriple(DWOFilenames.front()); 736 if (!ErrOrTriple) { 737 logAllUnhandledErrors(ErrOrTriple.takeError(), WithColor::error()); 738 return 1; 739 } 740 741 // Get the target. 742 const Target *TheTarget = 743 TargetRegistry::lookupTarget("", *ErrOrTriple, ErrorStr); 744 if (!TheTarget) 745 return error(ErrorStr, Context); 746 std::string TripleName = ErrOrTriple->getTriple(); 747 748 // Create all the MC Objects. 749 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 750 if (!MRI) 751 return error(Twine("no register info for target ") + TripleName, Context); 752 753 MCTargetOptions MCOptions = llvm::mc::InitMCTargetOptionsFromFlags(); 754 std::unique_ptr<MCAsmInfo> MAI( 755 TheTarget->createMCAsmInfo(*MRI, TripleName, MCOptions)); 756 if (!MAI) 757 return error("no asm info for target " + TripleName, Context); 758 759 MCObjectFileInfo MOFI; 760 MCContext MC(MAI.get(), MRI.get(), &MOFI); 761 MOFI.InitMCObjectFileInfo(*ErrOrTriple, /*PIC*/ false, MC); 762 763 std::unique_ptr<MCSubtargetInfo> MSTI( 764 TheTarget->createMCSubtargetInfo(TripleName, "", "")); 765 if (!MSTI) 766 return error("no subtarget info for target " + TripleName, Context); 767 768 MCTargetOptions Options; 769 auto MAB = TheTarget->createMCAsmBackend(*MSTI, *MRI, Options); 770 if (!MAB) 771 return error("no asm backend for target " + TripleName, Context); 772 773 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 774 if (!MII) 775 return error("no instr info info for target " + TripleName, Context); 776 777 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC); 778 if (!MCE) 779 return error("no code emitter for target " + TripleName, Context); 780 781 // Create the output file. 782 std::error_code EC; 783 ToolOutputFile OutFile(OutputFilename, EC, sys::fs::OF_None); 784 Optional<buffer_ostream> BOS; 785 raw_pwrite_stream *OS; 786 if (EC) 787 return error(Twine(OutputFilename) + ": " + EC.message(), Context); 788 if (OutFile.os().supportsSeeking()) { 789 OS = &OutFile.os(); 790 } else { 791 BOS.emplace(OutFile.os()); 792 OS = BOS.getPointer(); 793 } 794 795 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( 796 *ErrOrTriple, MC, std::unique_ptr<MCAsmBackend>(MAB), 797 MAB->createObjectWriter(*OS), std::unique_ptr<MCCodeEmitter>(MCE), *MSTI, 798 MCOptions.MCRelaxAll, MCOptions.MCIncrementalLinkerCompatible, 799 /*DWARFMustBeAtTheEnd*/ false)); 800 if (!MS) 801 return error("no object streamer for target " + TripleName, Context); 802 803 if (auto Err = write(*MS, DWOFilenames)) { 804 logAllUnhandledErrors(std::move(Err), WithColor::error()); 805 return 1; 806 } 807 808 MS->Finish(); 809 OutFile.keep(); 810 return 0; 811 } 812