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