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(uint32_t Form, DataExtractor InfoData, uint32_t &InfoOffset, 116 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 InfoData.getU32(&Offset); // Length 137 uint16_t Version = InfoData.getU16(&Offset); 138 InfoData.getU32(&Offset); // Abbrev offset (should be zero) 139 uint8_t AddrSize = InfoData.getU8(&Offset); 140 141 uint32_t AbbrCode = InfoData.getULEB128(&Offset); 142 143 DataExtractor AbbrevData(Abbrev, true, 0); 144 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode); 145 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset); 146 if (Tag != dwarf::DW_TAG_compile_unit) 147 return make_error<DWPError>("top level DIE is not a compile unit"); 148 // DW_CHILDREN 149 AbbrevData.getU8(&AbbrevOffset); 150 uint32_t Name; 151 uint32_t Form; 152 CompileUnitIdentifiers ID; 153 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) | 154 (Form = AbbrevData.getULEB128(&AbbrevOffset)) && 155 (Name != 0 || Form != 0)) { 156 switch (Name) { 157 case dwarf::DW_AT_name: { 158 Expected<const char *> EName = 159 getIndexedString(Form, InfoData, Offset, StrOffsets, Str); 160 if (!EName) 161 return EName.takeError(); 162 ID.Name = *EName; 163 break; 164 } 165 case dwarf::DW_AT_GNU_dwo_name: { 166 Expected<const char *> EName = 167 getIndexedString(Form, InfoData, Offset, StrOffsets, Str); 168 if (!EName) 169 return EName.takeError(); 170 ID.DWOName = *EName; 171 break; 172 } 173 case dwarf::DW_AT_GNU_dwo_id: 174 ID.Signature = InfoData.getU64(&Offset); 175 break; 176 default: 177 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize); 178 } 179 } 180 return ID; 181 } 182 183 struct UnitIndexEntry { 184 DWARFUnitIndex::Entry::SectionContribution Contributions[8]; 185 std::string Name; 186 std::string DWOName; 187 StringRef DWPName; 188 }; 189 190 static StringRef getSubsection(StringRef Section, 191 const DWARFUnitIndex::Entry &Entry, 192 DWARFSectionKind Kind) { 193 const auto *Off = Entry.getOffset(Kind); 194 if (!Off) 195 return StringRef(); 196 return Section.substr(Off->Offset, Off->Length); 197 } 198 199 static void addAllTypesFromDWP( 200 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries, 201 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types, 202 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) { 203 Out.SwitchSection(OutputTypes); 204 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) { 205 auto *I = E.getOffsets(); 206 if (!I) 207 continue; 208 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry)); 209 if (!P.second) 210 continue; 211 auto &Entry = P.first->second; 212 // Zero out the debug_info contribution 213 Entry.Contributions[0] = {}; 214 for (auto Kind : TUIndex.getColumnKinds()) { 215 auto &C = Entry.Contributions[Kind - DW_SECT_INFO]; 216 C.Offset += I->Offset; 217 C.Length = I->Length; 218 ++I; 219 } 220 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; 221 Out.EmitBytes(Types.substr( 222 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset, 223 C.Length)); 224 C.Offset = TypesOffset; 225 TypesOffset += C.Length; 226 } 227 } 228 229 static void addAllTypes(MCStreamer &Out, 230 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries, 231 MCSection *OutputTypes, 232 const std::vector<StringRef> &TypesSections, 233 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) { 234 for (StringRef Types : TypesSections) { 235 Out.SwitchSection(OutputTypes); 236 uint32_t Offset = 0; 237 DataExtractor Data(Types, true, 0); 238 while (Data.isValidOffset(Offset)) { 239 UnitIndexEntry Entry = CUEntry; 240 // Zero out the debug_info contribution 241 Entry.Contributions[0] = {}; 242 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; 243 C.Offset = TypesOffset; 244 auto PrevOffset = Offset; 245 // Length of the unit, including the 4 byte length field. 246 C.Length = Data.getU32(&Offset) + 4; 247 248 Data.getU16(&Offset); // Version 249 Data.getU32(&Offset); // Abbrev offset 250 Data.getU8(&Offset); // Address size 251 auto Signature = Data.getU64(&Offset); 252 Offset = PrevOffset + C.Length; 253 254 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry)); 255 if (!P.second) 256 continue; 257 258 Out.EmitBytes(Types.substr(PrevOffset, C.Length)); 259 TypesOffset += C.Length; 260 } 261 } 262 } 263 264 static void 265 writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets, 266 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries, 267 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) { 268 for (const auto &E : IndexEntries) 269 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i) 270 if (ContributionOffsets[i]) 271 Out.EmitIntValue(E.second.Contributions[i].*Field, 4); 272 } 273 274 static void 275 writeIndex(MCStreamer &Out, MCSection *Section, 276 ArrayRef<unsigned> ContributionOffsets, 277 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) { 278 if (IndexEntries.empty()) 279 return; 280 281 unsigned Columns = 0; 282 for (auto &C : ContributionOffsets) 283 if (C) 284 ++Columns; 285 286 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2)); 287 uint64_t Mask = Buckets.size() - 1; 288 size_t i = 0; 289 for (const auto &P : IndexEntries) { 290 auto S = P.first; 291 auto H = S & Mask; 292 auto HP = ((S >> 32) & Mask) | 1; 293 while (Buckets[H]) { 294 assert(S != IndexEntries.begin()[Buckets[H] - 1].first && 295 "Duplicate unit"); 296 H = (H + HP) & Mask; 297 } 298 Buckets[H] = i + 1; 299 ++i; 300 } 301 302 Out.SwitchSection(Section); 303 Out.EmitIntValue(2, 4); // Version 304 Out.EmitIntValue(Columns, 4); // Columns 305 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units 306 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets 307 308 // Write the signatures. 309 for (const auto &I : Buckets) 310 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8); 311 312 // Write the indexes. 313 for (const auto &I : Buckets) 314 Out.EmitIntValue(I, 4); 315 316 // Write the column headers (which sections will appear in the table) 317 for (size_t i = 0; i != ContributionOffsets.size(); ++i) 318 if (ContributionOffsets[i]) 319 Out.EmitIntValue(i + DW_SECT_INFO, 4); 320 321 // Write the offsets. 322 writeIndexTable(Out, ContributionOffsets, IndexEntries, 323 &DWARFUnitIndex::Entry::SectionContribution::Offset); 324 325 // Write the lengths. 326 writeIndexTable(Out, ContributionOffsets, IndexEntries, 327 &DWARFUnitIndex::Entry::SectionContribution::Length); 328 } 329 static bool consumeCompressedDebugSectionHeader(StringRef &data, 330 uint64_t &OriginalSize) { 331 // Consume "ZLIB" prefix. 332 if (!data.startswith("ZLIB")) 333 return false; 334 data = data.substr(4); 335 // Consume uncompressed section size (big-endian 8 bytes). 336 DataExtractor extractor(data, false, 8); 337 uint32_t Offset = 0; 338 OriginalSize = extractor.getU64(&Offset); 339 if (Offset == 0) 340 return false; 341 data = data.substr(Offset); 342 return true; 343 } 344 345 std::string buildDWODescription(StringRef Name, StringRef DWPName, StringRef DWOName) { 346 std::string Text = "\'"; 347 Text += Name; 348 Text += '\''; 349 if (!DWPName.empty()) { 350 Text += " (from "; 351 if (!DWOName.empty()) { 352 Text += '\''; 353 Text += DWOName; 354 Text += "' in "; 355 } 356 Text += '\''; 357 Text += DWPName; 358 Text += "')"; 359 } 360 return Text; 361 } 362 363 static Error handleCompressedSection( 364 std::deque<SmallString<32>> &UncompressedSections, StringRef &Name, 365 StringRef &Contents) { 366 if (!Name.startswith("zdebug_")) 367 return Error(); 368 UncompressedSections.emplace_back(); 369 uint64_t OriginalSize; 370 if (!zlib::isAvailable()) 371 return make_error<DWPError>("zlib not available"); 372 if (!consumeCompressedDebugSectionHeader(Contents, OriginalSize) || 373 zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) != 374 zlib::StatusOK) 375 return make_error<DWPError>( 376 ("failure while decompressing compressed section: '" + Name + "\'") 377 .str()); 378 Name = Name.substr(1); 379 Contents = UncompressedSections.back(); 380 return Error(); 381 } 382 383 static Error handleSection( 384 const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections, 385 const MCSection *StrSection, const MCSection *StrOffsetSection, 386 const MCSection *TypesSection, const MCSection *CUIndexSection, 387 const MCSection *TUIndexSection, const SectionRef &Section, MCStreamer &Out, 388 std::deque<SmallString<32>> &UncompressedSections, 389 uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry, 390 StringRef &CurStrSection, StringRef &CurStrOffsetSection, 391 std::vector<StringRef> &CurTypesSection, StringRef &InfoSection, 392 StringRef &AbbrevSection, StringRef &CurCUIndexSection, 393 StringRef &CurTUIndexSection) { 394 if (Section.isBSS()) 395 return Error(); 396 397 if (Section.isVirtual()) 398 return Error(); 399 400 StringRef Name; 401 if (std::error_code Err = Section.getName(Name)) 402 return errorCodeToError(Err); 403 404 Name = Name.substr(Name.find_first_not_of("._")); 405 406 StringRef Contents; 407 if (auto Err = Section.getContents(Contents)) 408 return errorCodeToError(Err); 409 410 if (auto Err = handleCompressedSection(UncompressedSections, Name, Contents)) 411 return Err; 412 413 auto SectionPair = KnownSections.find(Name); 414 if (SectionPair == KnownSections.end()) 415 return Error(); 416 417 if (DWARFSectionKind Kind = SectionPair->second.second) { 418 auto Index = Kind - DW_SECT_INFO; 419 if (Kind != DW_SECT_TYPES) { 420 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index]; 421 ContributionOffsets[Index] += 422 (CurEntry.Contributions[Index].Length = Contents.size()); 423 } 424 425 switch (Kind) { 426 case DW_SECT_INFO: 427 InfoSection = Contents; 428 break; 429 case DW_SECT_ABBREV: 430 AbbrevSection = Contents; 431 break; 432 default: 433 break; 434 } 435 } 436 437 MCSection *OutSection = SectionPair->second.first; 438 if (OutSection == StrOffsetSection) 439 CurStrOffsetSection = Contents; 440 else if (OutSection == StrSection) 441 CurStrSection = Contents; 442 else if (OutSection == TypesSection) 443 CurTypesSection.push_back(Contents); 444 else if (OutSection == CUIndexSection) 445 CurCUIndexSection = Contents; 446 else if (OutSection == TUIndexSection) 447 CurTUIndexSection = Contents; 448 else { 449 Out.SwitchSection(OutSection); 450 Out.EmitBytes(Contents); 451 } 452 return Error(); 453 } 454 455 static Error 456 buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE, 457 const CompileUnitIdentifiers &ID, StringRef DWPName) { 458 return make_error<DWPError>( 459 std::string("Duplicate DWO ID (") + utohexstr(PrevE.first) + ") in " + 460 buildDWODescription(PrevE.second.Name, PrevE.second.DWPName, 461 PrevE.second.DWOName) + 462 " and " + buildDWODescription(ID.Name, DWPName, ID.DWOName)); 463 } 464 465 static Error write(MCStreamer &Out, ArrayRef<std::string> Inputs) { 466 const auto &MCOFI = *Out.getContext().getObjectFileInfo(); 467 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection(); 468 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection(); 469 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection(); 470 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection(); 471 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection(); 472 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = { 473 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}}, 474 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}}, 475 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}}, 476 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}}, 477 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}}, 478 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}}, 479 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}, 480 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}}, 481 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}}; 482 483 MapVector<uint64_t, UnitIndexEntry> IndexEntries; 484 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries; 485 486 uint32_t ContributionOffsets[8] = {}; 487 488 DWPStringPool Strings(Out, StrSection); 489 490 SmallVector<OwningBinary<object::ObjectFile>, 128> Objects; 491 Objects.reserve(Inputs.size()); 492 493 std::deque<SmallString<32>> UncompressedSections; 494 495 for (const auto &Input : Inputs) { 496 auto ErrOrObj = object::ObjectFile::createObjectFile(Input); 497 if (!ErrOrObj) 498 return ErrOrObj.takeError(); 499 500 auto &Obj = *ErrOrObj->getBinary(); 501 Objects.push_back(std::move(*ErrOrObj)); 502 503 UnitIndexEntry CurEntry = {}; 504 505 StringRef CurStrSection; 506 StringRef CurStrOffsetSection; 507 std::vector<StringRef> CurTypesSection; 508 StringRef InfoSection; 509 StringRef AbbrevSection; 510 StringRef CurCUIndexSection; 511 StringRef CurTUIndexSection; 512 513 for (const auto &Section : Obj.sections()) 514 if (auto Err = handleSection( 515 KnownSections, StrSection, StrOffsetSection, TypesSection, 516 CUIndexSection, TUIndexSection, Section, Out, 517 UncompressedSections, ContributionOffsets, CurEntry, 518 CurStrSection, CurStrOffsetSection, CurTypesSection, InfoSection, 519 AbbrevSection, CurCUIndexSection, CurTUIndexSection)) 520 return Err; 521 522 if (InfoSection.empty()) 523 continue; 524 525 writeStringsAndOffsets(Out, Strings, StrOffsetSection, CurStrSection, 526 CurStrOffsetSection); 527 528 if (CurCUIndexSection.empty()) { 529 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers( 530 AbbrevSection, InfoSection, CurStrOffsetSection, CurStrSection); 531 if (!EID) 532 return EID.takeError(); 533 const auto &ID = *EID; 534 auto P = IndexEntries.insert(std::make_pair(ID.Signature, CurEntry)); 535 if (!P.second) 536 return buildDuplicateError(*P.first, ID, ""); 537 P.first->second.Name = ID.Name; 538 P.first->second.DWOName = ID.DWOName; 539 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, 540 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); 541 continue; 542 } 543 544 DWARFUnitIndex CUIndex(DW_SECT_INFO); 545 DataExtractor CUIndexData(CurCUIndexSection, Obj.isLittleEndian(), 0); 546 if (!CUIndex.parse(CUIndexData)) 547 return make_error<DWPError>("Failed to parse cu_index"); 548 549 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) { 550 auto *I = E.getOffsets(); 551 if (!I) 552 continue; 553 auto P = IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry)); 554 Expected<CompileUnitIdentifiers> EID = getCUIdentifiers( 555 getSubsection(AbbrevSection, E, DW_SECT_ABBREV), 556 getSubsection(InfoSection, E, DW_SECT_INFO), 557 getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS), 558 CurStrSection); 559 if (!EID) 560 return EID.takeError(); 561 const auto &ID = *EID; 562 if (!P.second) 563 return buildDuplicateError(*P.first, ID, Input); 564 auto &NewEntry = P.first->second; 565 NewEntry.Name = ID.Name; 566 NewEntry.DWOName = ID.DWOName; 567 NewEntry.DWPName = Input; 568 for (auto Kind : CUIndex.getColumnKinds()) { 569 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO]; 570 C.Offset += I->Offset; 571 C.Length = I->Length; 572 ++I; 573 } 574 } 575 576 if (!CurTypesSection.empty()) { 577 if (CurTypesSection.size() != 1) 578 return make_error<DWPError>("multiple type unit sections in .dwp file"); 579 DWARFUnitIndex TUIndex(DW_SECT_TYPES); 580 DataExtractor TUIndexData(CurTUIndexSection, Obj.isLittleEndian(), 0); 581 if (!TUIndex.parse(TUIndexData)) 582 return make_error<DWPError>("Failed to parse tu_index"); 583 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection, 584 CurTypesSection.front(), CurEntry, 585 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); 586 } 587 } 588 589 // Lie about there being no info contributions so the TU index only includes 590 // the type unit contribution 591 ContributionOffsets[0] = 0; 592 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets, 593 TypeIndexEntries); 594 595 // Lie about the type contribution 596 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0; 597 // Unlie about the info contribution 598 ContributionOffsets[0] = 1; 599 600 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets, 601 IndexEntries); 602 603 return Error(); 604 } 605 606 static int error(const Twine &Error, const Twine &Context) { 607 errs() << Twine("while processing ") + Context + ":\n"; 608 errs() << Twine("error: ") + Error + "\n"; 609 return 1; 610 } 611 612 int main(int argc, char **argv) { 613 614 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files"); 615 616 llvm::InitializeAllTargetInfos(); 617 llvm::InitializeAllTargetMCs(); 618 llvm::InitializeAllTargets(); 619 llvm::InitializeAllAsmPrinters(); 620 621 std::string ErrorStr; 622 StringRef Context = "dwarf streamer init"; 623 624 Triple TheTriple("x86_64-linux-gnu"); 625 626 // Get the target. 627 const Target *TheTarget = 628 TargetRegistry::lookupTarget("", TheTriple, ErrorStr); 629 if (!TheTarget) 630 return error(ErrorStr, Context); 631 std::string TripleName = TheTriple.getTriple(); 632 633 // Create all the MC Objects. 634 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 635 if (!MRI) 636 return error(Twine("no register info for target ") + TripleName, Context); 637 638 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); 639 if (!MAI) 640 return error("no asm info for target " + TripleName, Context); 641 642 MCObjectFileInfo MOFI; 643 MCContext MC(MAI.get(), MRI.get(), &MOFI); 644 MOFI.InitMCObjectFileInfo(TheTriple, /*PIC*/ false, CodeModel::Default, MC); 645 646 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); 647 if (!MAB) 648 return error("no asm backend for target " + TripleName, Context); 649 650 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 651 if (!MII) 652 return error("no instr info info for target " + TripleName, Context); 653 654 std::unique_ptr<MCSubtargetInfo> MSTI( 655 TheTarget->createMCSubtargetInfo(TripleName, "", "")); 656 if (!MSTI) 657 return error("no subtarget info for target " + TripleName, Context); 658 659 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC); 660 if (!MCE) 661 return error("no code emitter for target " + TripleName, Context); 662 663 // Create the output file. 664 std::error_code EC; 665 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None); 666 if (EC) 667 return error(Twine(OutputFilename) + ": " + EC.message(), Context); 668 669 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); 670 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( 671 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll, 672 MCOptions.MCIncrementalLinkerCompatible, 673 /*DWARFMustBeAtTheEnd*/ false)); 674 if (!MS) 675 return error("no object streamer for target " + TripleName, Context); 676 677 if (auto Err = write(*MS, InputFiles)) { 678 logAllUnhandledErrors(std::move(Err), errs(), "error: "); 679 return 1; 680 } 681 682 MS->Finish(); 683 } 684