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