1 #include "llvm/ADT/STLExtras.h" 2 #include "llvm/ADT/StringSet.h" 3 #include "llvm/CodeGen/AsmPrinter.h" 4 #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" 5 #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" 6 #include "llvm/MC/MCAsmInfo.h" 7 #include "llvm/MC/MCContext.h" 8 #include "llvm/MC/MCInstrInfo.h" 9 #include "llvm/MC/MCObjectFileInfo.h" 10 #include "llvm/MC/MCRegisterInfo.h" 11 #include "llvm/MC/MCSectionELF.h" 12 #include "llvm/MC/MCStreamer.h" 13 #include "llvm/MC/MCTargetOptionsCommandFlags.h" 14 #include "llvm/Object/ObjectFile.h" 15 #include "llvm/Support/DataExtractor.h" 16 #include "llvm/Support/FileSystem.h" 17 #include "llvm/Support/MathExtras.h" 18 #include "llvm/Support/MemoryBuffer.h" 19 #include "llvm/Support/Options.h" 20 #include "llvm/Support/TargetRegistry.h" 21 #include "llvm/Support/TargetSelect.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include "llvm/Target/TargetMachine.h" 24 #include <list> 25 #include <memory> 26 #include <unordered_set> 27 28 using namespace llvm; 29 using namespace llvm::object; 30 using namespace cl; 31 32 OptionCategory DwpCategory("Specific Options"); 33 static list<std::string> InputFiles(Positional, OneOrMore, 34 desc("<input files>"), cat(DwpCategory)); 35 36 static opt<std::string> OutputFilename(Required, "o", 37 desc("Specify the output file."), 38 value_desc("filename"), 39 cat(DwpCategory)); 40 41 static int error(const Twine &Error, const Twine &Context) { 42 errs() << Twine("while processing ") + Context + ":\n"; 43 errs() << Twine("error: ") + Error + "\n"; 44 return 1; 45 } 46 47 static std::error_code 48 writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings, 49 uint32_t &StringOffset, MCSection *StrSection, 50 MCSection *StrOffsetSection, StringRef CurStrSection, 51 StringRef CurStrOffsetSection) { 52 // Could possibly produce an error or warning if one of these was non-null but 53 // the other was null. 54 if (CurStrSection.empty() || CurStrOffsetSection.empty()) 55 return std::error_code(); 56 57 DenseMap<uint32_t, uint32_t> OffsetRemapping; 58 59 DataExtractor Data(CurStrSection, true, 0); 60 uint32_t LocalOffset = 0; 61 uint32_t PrevOffset = 0; 62 while (const char *s = Data.getCStr(&LocalOffset)) { 63 StringRef Str(s, LocalOffset - PrevOffset - 1); 64 auto Pair = Strings.insert(std::make_pair(Str, StringOffset)); 65 if (Pair.second) { 66 Out.SwitchSection(StrSection); 67 Out.EmitBytes( 68 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1)); 69 StringOffset += Str.size() + 1; 70 } 71 OffsetRemapping[PrevOffset] = Pair.first->second; 72 PrevOffset = LocalOffset; 73 } 74 75 Data = DataExtractor(CurStrOffsetSection, true, 0); 76 77 Out.SwitchSection(StrOffsetSection); 78 79 uint32_t Offset = 0; 80 uint64_t Size = CurStrOffsetSection.size(); 81 while (Offset < Size) { 82 auto OldOffset = Data.getU32(&Offset); 83 auto NewOffset = OffsetRemapping[OldOffset]; 84 Out.EmitIntValue(NewOffset, 4); 85 } 86 87 return std::error_code(); 88 } 89 90 static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) { 91 uint64_t CurCode; 92 uint32_t Offset = 0; 93 DataExtractor AbbrevData(Abbrev, true, 0); 94 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) { 95 // Tag 96 AbbrevData.getULEB128(&Offset); 97 // DW_CHILDREN 98 AbbrevData.getU8(&Offset); 99 // Attributes 100 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset)) 101 ; 102 } 103 return Offset; 104 } 105 106 static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) { 107 uint32_t Offset = 0; 108 DataExtractor InfoData(Info, true, 0); 109 InfoData.getU32(&Offset); // Length 110 uint16_t Version = InfoData.getU16(&Offset); 111 InfoData.getU32(&Offset); // Abbrev offset (should be zero) 112 uint8_t AddrSize = InfoData.getU8(&Offset); 113 114 uint32_t AbbrCode = InfoData.getULEB128(&Offset); 115 116 DataExtractor AbbrevData(Abbrev, true, 0); 117 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode); 118 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset); 119 (void)Tag; 120 // FIXME: Real error handling 121 assert(Tag == dwarf::DW_TAG_compile_unit); 122 // DW_CHILDREN 123 AbbrevData.getU8(&AbbrevOffset); 124 uint32_t Name; 125 uint32_t Form; 126 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) | 127 (Form = AbbrevData.getULEB128(&AbbrevOffset)) && 128 Name != dwarf::DW_AT_GNU_dwo_id) { 129 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize); 130 } 131 // FIXME: Real error handling 132 assert(Name == dwarf::DW_AT_GNU_dwo_id); 133 return InfoData.getU64(&Offset); 134 } 135 136 struct UnitIndexEntry { 137 uint64_t Signature; 138 DWARFUnitIndex::Entry::SectionContribution Contributions[8]; 139 }; 140 141 static void addAllTypes(MCStreamer &Out, 142 std::vector<UnitIndexEntry> &TypeIndexEntries, 143 MCSection *OutputTypes, StringRef Types, 144 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) { 145 if (Types.empty()) 146 return; 147 148 Out.SwitchSection(OutputTypes); 149 uint32_t Offset = 0; 150 DataExtractor Data(Types, true, 0); 151 while (Data.isValidOffset(Offset)) { 152 UnitIndexEntry Entry = CUEntry; 153 // Zero out the debug_info contribution 154 Entry.Contributions[0] = {}; 155 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; 156 C.Offset = TypesOffset; 157 auto PrevOffset = Offset; 158 // Length of the unit, including the 4 byte length field. 159 C.Length = Data.getU32(&Offset) + 4; 160 161 Data.getU16(&Offset); // Version 162 Data.getU32(&Offset); // Abbrev offset 163 Data.getU8(&Offset); // Address size 164 Entry.Signature = Data.getU64(&Offset); 165 Offset = PrevOffset + C.Length; 166 167 if (any_of(TypeIndexEntries, [&](const UnitIndexEntry &E) { 168 return E.Signature == Entry.Signature; 169 })) 170 continue; 171 172 Out.EmitBytes(Types.substr(PrevOffset, C.Length)); 173 TypesOffset += C.Length; 174 175 TypeIndexEntries.push_back(Entry); 176 } 177 } 178 179 static void 180 writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets, 181 ArrayRef<UnitIndexEntry> IndexEntries, 182 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) { 183 for (const auto &E : IndexEntries) 184 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i) 185 if (ContributionOffsets[i]) 186 Out.EmitIntValue(E.Contributions[i].*Field, 4); 187 } 188 189 static void writeIndex(MCStreamer &Out, MCSection *Section, 190 ArrayRef<unsigned> ContributionOffsets, 191 ArrayRef<UnitIndexEntry> IndexEntries) { 192 unsigned Columns = 0; 193 for (auto &C : ContributionOffsets) 194 if (C) 195 ++Columns; 196 197 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2)); 198 uint64_t Mask = Buckets.size() - 1; 199 for (size_t i = 0; i != IndexEntries.size(); ++i) { 200 auto S = IndexEntries[i].Signature; 201 auto H = S & Mask; 202 while (Buckets[H]) { 203 assert(S != IndexEntries[Buckets[H] - 1].Signature && 204 "Duplicate type unit"); 205 H += ((S >> 32) & Mask) | 1; 206 } 207 Buckets[H] = i + 1; 208 } 209 210 Out.SwitchSection(Section); 211 Out.EmitIntValue(2, 4); // Version 212 Out.EmitIntValue(Columns, 4); // Columns 213 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units 214 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets 215 216 // Write the signatures. 217 for (const auto &I : Buckets) 218 Out.EmitIntValue(I ? IndexEntries[I - 1].Signature : 0, 8); 219 220 // Write the indexes. 221 for (const auto &I : Buckets) 222 Out.EmitIntValue(I, 4); 223 224 // Write the column headers (which sections will appear in the table) 225 for (size_t i = 0; i != ContributionOffsets.size(); ++i) 226 if (ContributionOffsets[i]) 227 Out.EmitIntValue(i + DW_SECT_INFO, 4); 228 229 // Write the offsets. 230 writeIndexTable(Out, ContributionOffsets, IndexEntries, 231 &DWARFUnitIndex::Entry::SectionContribution::Offset); 232 233 // Write the lengths. 234 writeIndexTable(Out, ContributionOffsets, IndexEntries, 235 &DWARFUnitIndex::Entry::SectionContribution::Length); 236 } 237 static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) { 238 const auto &MCOFI = *Out.getContext().getObjectFileInfo(); 239 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection(); 240 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection(); 241 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection(); 242 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = { 243 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}}, 244 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}}, 245 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}}, 246 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}}, 247 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}}, 248 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}}, 249 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}}; 250 251 std::vector<UnitIndexEntry> IndexEntries; 252 std::vector<UnitIndexEntry> TypeIndexEntries; 253 254 StringMap<uint32_t> Strings; 255 uint32_t StringOffset = 0; 256 257 uint32_t ContributionOffsets[8] = {}; 258 259 for (const auto &Input : Inputs) { 260 auto ErrOrObj = object::ObjectFile::createObjectFile(Input); 261 if (!ErrOrObj) 262 return ErrOrObj.getError(); 263 264 IndexEntries.emplace_back(); 265 UnitIndexEntry &CurEntry = IndexEntries.back(); 266 267 StringRef CurStrSection; 268 StringRef CurStrOffsetSection; 269 StringRef CurTypesSection; 270 StringRef InfoSection; 271 StringRef AbbrevSection; 272 273 for (const auto &Section : ErrOrObj->getBinary()->sections()) { 274 StringRef Name; 275 if (std::error_code Err = Section.getName(Name)) 276 return Err; 277 278 auto SectionPair = 279 KnownSections.find(Name.substr(Name.find_first_not_of("._"))); 280 if (SectionPair == KnownSections.end()) 281 continue; 282 283 StringRef Contents; 284 if (auto Err = Section.getContents(Contents)) 285 return Err; 286 287 if (DWARFSectionKind Kind = SectionPair->second.second) { 288 auto Index = Kind - DW_SECT_INFO; 289 if (Kind != DW_SECT_TYPES) { 290 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index]; 291 ContributionOffsets[Index] += 292 (CurEntry.Contributions[Index].Length = Contents.size()); 293 } 294 295 switch (Kind) { 296 case DW_SECT_INFO: 297 InfoSection = Contents; 298 break; 299 case DW_SECT_ABBREV: 300 AbbrevSection = Contents; 301 break; 302 default: 303 break; 304 } 305 } 306 307 MCSection *OutSection = SectionPair->second.first; 308 if (OutSection == StrOffsetSection) 309 CurStrOffsetSection = Contents; 310 else if (OutSection == StrSection) 311 CurStrSection = Contents; 312 else if (OutSection == TypesSection) 313 CurTypesSection = Contents; 314 else { 315 Out.SwitchSection(OutSection); 316 Out.EmitBytes(Contents); 317 } 318 } 319 320 assert(!AbbrevSection.empty()); 321 assert(!InfoSection.empty()); 322 CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection); 323 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, CurEntry, 324 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); 325 326 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset, 327 StrSection, StrOffsetSection, 328 CurStrSection, CurStrOffsetSection)) 329 return Err; 330 } 331 332 if (!TypeIndexEntries.empty()) { 333 // Lie about there being no info contributions so the TU index only includes 334 // the type unit contribution 335 ContributionOffsets[0] = 0; 336 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets, 337 TypeIndexEntries); 338 } 339 340 // Lie about the type contribution 341 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0; 342 // Unlie about the info contribution 343 ContributionOffsets[0] = 1; 344 345 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets, 346 IndexEntries); 347 348 return std::error_code(); 349 } 350 351 int main(int argc, char **argv) { 352 353 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files"); 354 355 llvm::InitializeAllTargetInfos(); 356 llvm::InitializeAllTargetMCs(); 357 llvm::InitializeAllTargets(); 358 llvm::InitializeAllAsmPrinters(); 359 360 std::string ErrorStr; 361 StringRef Context = "dwarf streamer init"; 362 363 Triple TheTriple("x86_64-linux-gnu"); 364 365 // Get the target. 366 const Target *TheTarget = 367 TargetRegistry::lookupTarget("", TheTriple, ErrorStr); 368 if (!TheTarget) 369 return error(ErrorStr, Context); 370 std::string TripleName = TheTriple.getTriple(); 371 372 // Create all the MC Objects. 373 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); 374 if (!MRI) 375 return error(Twine("no register info for target ") + TripleName, Context); 376 377 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); 378 if (!MAI) 379 return error("no asm info for target " + TripleName, Context); 380 381 MCObjectFileInfo MOFI; 382 MCContext MC(MAI.get(), MRI.get(), &MOFI); 383 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC); 384 385 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); 386 if (!MAB) 387 return error("no asm backend for target " + TripleName, Context); 388 389 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); 390 if (!MII) 391 return error("no instr info info for target " + TripleName, Context); 392 393 std::unique_ptr<MCSubtargetInfo> MSTI( 394 TheTarget->createMCSubtargetInfo(TripleName, "", "")); 395 if (!MSTI) 396 return error("no subtarget info for target " + TripleName, Context); 397 398 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC); 399 if (!MCE) 400 return error("no code emitter for target " + TripleName, Context); 401 402 // Create the output file. 403 std::error_code EC; 404 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None); 405 if (EC) 406 return error(Twine(OutputFilename) + ": " + EC.message(), Context); 407 408 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); 409 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( 410 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll, 411 MCOptions.MCIncrementalLinkerCompatible, 412 /*DWARFMustBeAtTheEnd*/ false)); 413 if (!MS) 414 return error("no object streamer for target " + TripleName, Context); 415 416 if (auto Err = write(*MS, InputFiles)) 417 return error(Err.message(), "Writing DWP file"); 418 419 MS->Finish(); 420 } 421