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