David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 1 | #include "llvm/ADT/STLExtras.h" |
| 2 | #include "llvm/ADT/StringSet.h" |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 3 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 4 | #include "llvm/CodeGen/AsmPrinter.h" |
| 5 | #include "llvm/MC/MCAsmInfo.h" |
| 6 | #include "llvm/MC/MCContext.h" |
| 7 | #include "llvm/MC/MCInstrInfo.h" |
| 8 | #include "llvm/MC/MCObjectFileInfo.h" |
| 9 | #include "llvm/MC/MCRegisterInfo.h" |
| 10 | #include "llvm/MC/MCSectionELF.h" |
| 11 | #include "llvm/MC/MCStreamer.h" |
| 12 | #include "llvm/Object/ObjectFile.h" |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 13 | #include "llvm/Support/DataExtractor.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Options.h" |
| 15 | #include "llvm/Support/FileSystem.h" |
| 16 | #include "llvm/Support/MemoryBuffer.h" |
| 17 | #include "llvm/Support/TargetRegistry.h" |
| 18 | #include "llvm/Support/raw_ostream.h" |
| 19 | #include "llvm/Target/TargetMachine.h" |
| 20 | #include "llvm/Support/TargetSelect.h" |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 21 | #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 22 | #include <memory> |
| 23 | #include <list> |
| 24 | #include <unordered_set> |
| 25 | |
| 26 | using namespace llvm; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 27 | using namespace llvm::object; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 28 | using namespace cl; |
| 29 | |
| 30 | OptionCategory DwpCategory("Specific Options"); |
| 31 | static list<std::string> InputFiles(Positional, OneOrMore, |
| 32 | desc("<input files>"), cat(DwpCategory)); |
| 33 | |
| 34 | static opt<std::string> OutputFilename(Required, "o", desc("Specify the output file."), |
| 35 | value_desc("filename"), cat(DwpCategory)); |
| 36 | |
| 37 | static int error(const Twine &Error, const Twine &Context) { |
| 38 | errs() << Twine("while processing ") + Context + ":\n"; |
| 39 | errs() << Twine("error: ") + Error + "\n"; |
| 40 | return 1; |
| 41 | } |
| 42 | |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 43 | static std::error_code |
| 44 | writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings, |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 45 | uint32_t &StringOffset, MCSection *StrSection, |
| 46 | MCSection *StrOffsetSection, StringRef CurStrSection, |
| 47 | StringRef CurStrOffsetSection) { |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 48 | // Could possibly produce an error or warning if one of these was non-null but |
| 49 | // the other was null. |
| 50 | if (CurStrSection.empty() || CurStrOffsetSection.empty()) |
| 51 | return std::error_code(); |
| 52 | |
| 53 | DenseMap<uint32_t, uint32_t> OffsetRemapping; |
| 54 | |
| 55 | DataExtractor Data(CurStrSection, true, 0); |
| 56 | uint32_t LocalOffset = 0; |
| 57 | uint32_t PrevOffset = 0; |
| 58 | while (const char *s = Data.getCStr(&LocalOffset)) { |
| 59 | StringRef Str(s, LocalOffset - PrevOffset - 1); |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 60 | auto Pair = Strings.insert(std::make_pair(Str, StringOffset)); |
| 61 | if (Pair.second) { |
| 62 | Out.SwitchSection(StrSection); |
| 63 | Out.EmitBytes( |
| 64 | StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1)); |
| 65 | StringOffset += Str.size() + 1; |
| 66 | } |
| 67 | OffsetRemapping[PrevOffset] = Pair.first->second; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 68 | PrevOffset = LocalOffset; |
| 69 | } |
| 70 | |
| 71 | Data = DataExtractor(CurStrOffsetSection, true, 0); |
| 72 | |
| 73 | Out.SwitchSection(StrOffsetSection); |
| 74 | |
| 75 | uint32_t Offset = 0; |
| 76 | uint64_t Size = CurStrOffsetSection.size(); |
| 77 | while (Offset < Size) { |
| 78 | auto OldOffset = Data.getU32(&Offset); |
| 79 | auto NewOffset = OffsetRemapping[OldOffset]; |
| 80 | Out.EmitIntValue(NewOffset, 4); |
| 81 | } |
| 82 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 83 | return std::error_code(); |
| 84 | } |
| 85 | |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 86 | static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) { |
| 87 | uint64_t CurCode; |
| 88 | uint32_t Offset = 0; |
| 89 | DataExtractor AbbrevData(Abbrev, true, 0); |
| 90 | while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) { |
| 91 | // Tag |
| 92 | AbbrevData.getULEB128(&Offset); |
| 93 | // DW_CHILDREN |
| 94 | AbbrevData.getU8(&Offset); |
| 95 | // Attributes |
| 96 | while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset)) |
| 97 | ; |
| 98 | } |
| 99 | return Offset; |
| 100 | } |
| 101 | |
| 102 | static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) { |
| 103 | uint32_t Offset = 0; |
| 104 | DataExtractor InfoData(Info, true, 0); |
| 105 | InfoData.getU32(&Offset); // Length |
| 106 | uint16_t Version = InfoData.getU16(&Offset); |
| 107 | InfoData.getU32(&Offset); // Abbrev offset (should be zero) |
| 108 | uint8_t AddrSize = InfoData.getU8(&Offset); |
| 109 | |
| 110 | uint32_t AbbrCode = InfoData.getULEB128(&Offset); |
| 111 | |
| 112 | DataExtractor AbbrevData(Abbrev, true, 0); |
| 113 | uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode); |
| 114 | uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset); |
| 115 | (void)Tag; |
| 116 | // FIXME: Real error handling |
| 117 | assert(Tag == dwarf::DW_TAG_compile_unit); |
| 118 | // DW_CHILDREN |
| 119 | AbbrevData.getU8(&AbbrevOffset); |
| 120 | uint32_t Name; |
| 121 | uint32_t Form; |
| 122 | while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) | |
| 123 | (Form = AbbrevData.getULEB128(&AbbrevOffset)) && |
| 124 | Name != dwarf::DW_AT_GNU_dwo_id) { |
| 125 | DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize); |
| 126 | } |
| 127 | // FIXME: Real error handling |
| 128 | assert(Name == dwarf::DW_AT_GNU_dwo_id); |
| 129 | return InfoData.getU64(&Offset); |
| 130 | } |
| 131 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 132 | static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) { |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 133 | const auto &MCOFI = *Out.getContext().getObjectFileInfo(); |
| 134 | MCSection *const StrSection = MCOFI.getDwarfStrDWOSection(); |
| 135 | MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection(); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 136 | const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = { |
| 137 | {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}}, |
| 138 | {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}}, |
| 139 | {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}}, |
| 140 | {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}}, |
| 141 | {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}}, |
David Blaikie | b702025 | 2015-12-04 21:16:42 +0000 | [diff] [blame^] | 142 | {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}}, |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 143 | {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}}; |
| 144 | |
| 145 | struct UnitIndexEntry { |
| 146 | uint64_t Signature; |
| 147 | DWARFUnitIndex::Entry::SectionContribution Contributions[8]; |
| 148 | }; |
| 149 | |
| 150 | std::vector<UnitIndexEntry> IndexEntries; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 151 | |
| 152 | StringMap<uint32_t> Strings; |
| 153 | uint32_t StringOffset = 0; |
| 154 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 155 | uint32_t ContributionOffsets[8] = {}; |
| 156 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 157 | for (const auto &Input : Inputs) { |
| 158 | auto ErrOrObj = object::ObjectFile::createObjectFile(Input); |
| 159 | if (!ErrOrObj) |
| 160 | return ErrOrObj.getError(); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 161 | |
| 162 | IndexEntries.emplace_back(); |
| 163 | UnitIndexEntry &CurEntry = IndexEntries.back(); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 164 | |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 165 | StringRef CurStrSection; |
| 166 | StringRef CurStrOffsetSection; |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 167 | StringRef InfoSection; |
| 168 | StringRef AbbrevSection; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 169 | |
| 170 | for (const auto &Section : ErrOrObj->getBinary()->sections()) { |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 171 | StringRef Name; |
| 172 | if (std::error_code Err = Section.getName(Name)) |
| 173 | return Err; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 174 | |
| 175 | auto SectionPair = |
| 176 | KnownSections.find(Name.substr(Name.find_first_not_of("._"))); |
| 177 | if (SectionPair == KnownSections.end()) |
| 178 | continue; |
| 179 | |
| 180 | StringRef Contents; |
| 181 | if (auto Err = Section.getContents(Contents)) |
| 182 | return Err; |
| 183 | |
| 184 | if (DWARFSectionKind Kind = SectionPair->second.second) { |
| 185 | auto Index = Kind - DW_SECT_INFO; |
| 186 | CurEntry.Contributions[Index].Offset = ContributionOffsets[Index]; |
| 187 | ContributionOffsets[Index] += |
| 188 | (CurEntry.Contributions[Index].Length = Contents.size()); |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 189 | |
| 190 | if (Kind == DW_SECT_INFO) { |
| 191 | assert(InfoSection.empty()); |
| 192 | InfoSection = Contents; |
| 193 | } else if (Kind == DW_SECT_ABBREV) { |
| 194 | assert(AbbrevSection.empty()); |
| 195 | AbbrevSection = Contents; |
| 196 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | MCSection *OutSection = SectionPair->second.first; |
| 200 | if (OutSection == StrOffsetSection) |
| 201 | CurStrOffsetSection = Contents; |
| 202 | else if (OutSection == StrSection) |
| 203 | CurStrSection = Contents; |
| 204 | else { |
| 205 | Out.SwitchSection(OutSection); |
| 206 | Out.EmitBytes(Contents); |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 207 | } |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 208 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 209 | |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 210 | assert(!AbbrevSection.empty()); |
| 211 | assert(!InfoSection.empty()); |
| 212 | CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection); |
| 213 | |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 214 | if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset, |
| 215 | StrSection, StrOffsetSection, |
| 216 | CurStrSection, CurStrOffsetSection)) |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 217 | return Err; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 218 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 219 | |
David Blaikie | b3757c0 | 2015-12-02 22:01:56 +0000 | [diff] [blame] | 220 | unsigned Columns = 0; |
| 221 | for (auto &C : ContributionOffsets) |
| 222 | if (C) |
| 223 | ++Columns; |
| 224 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 225 | Out.SwitchSection(MCOFI.getDwarfCUIndexSection()); |
| 226 | Out.EmitIntValue(2, 4); // Version |
David Blaikie | b3757c0 | 2015-12-02 22:01:56 +0000 | [diff] [blame] | 227 | Out.EmitIntValue(Columns, 4); // Columns |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 228 | Out.EmitIntValue(IndexEntries.size(), 4); // Num Units |
| 229 | // FIXME: This is not the right number of buckets for a real hash. |
| 230 | Out.EmitIntValue(IndexEntries.size(), 4); // Num Buckets |
| 231 | |
| 232 | // Write the signatures. |
| 233 | for (const auto &E : IndexEntries) |
| 234 | Out.EmitIntValue(E.Signature, 8); |
| 235 | |
| 236 | // Write the indexes. |
| 237 | for (size_t i = 0; i != IndexEntries.size(); ++i) |
| 238 | Out.EmitIntValue(i + 1, 4); |
| 239 | |
| 240 | // Write the column headers (which sections will appear in the table) |
David Blaikie | b3757c0 | 2015-12-02 22:01:56 +0000 | [diff] [blame] | 241 | for (size_t i = 0; i != array_lengthof(ContributionOffsets); ++i) |
| 242 | if (ContributionOffsets[i]) |
| 243 | Out.EmitIntValue(i + DW_SECT_INFO, 4); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 244 | |
| 245 | // Write the offsets. |
| 246 | for (const auto &E : IndexEntries) |
David Blaikie | b3757c0 | 2015-12-02 22:01:56 +0000 | [diff] [blame] | 247 | for (size_t i = 0; i != array_lengthof(E.Contributions); ++i) |
| 248 | if (ContributionOffsets[i]) |
| 249 | Out.EmitIntValue(E.Contributions[i].Offset, 4); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 250 | |
| 251 | // Write the lengths. |
| 252 | for (const auto &E : IndexEntries) |
David Blaikie | b3757c0 | 2015-12-02 22:01:56 +0000 | [diff] [blame] | 253 | for (size_t i = 0; i != array_lengthof(E.Contributions); ++i) |
| 254 | if (ContributionOffsets[i]) |
| 255 | Out.EmitIntValue(E.Contributions[i].Length, 4); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 256 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 257 | return std::error_code(); |
| 258 | } |
| 259 | |
| 260 | int main(int argc, char** argv) { |
| 261 | |
| 262 | ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files"); |
| 263 | |
| 264 | llvm::InitializeAllTargetInfos(); |
| 265 | llvm::InitializeAllTargetMCs(); |
| 266 | llvm::InitializeAllTargets(); |
| 267 | llvm::InitializeAllAsmPrinters(); |
| 268 | |
| 269 | std::string ErrorStr; |
| 270 | StringRef Context = "dwarf streamer init"; |
| 271 | |
| 272 | Triple TheTriple("x86_64-linux-gnu"); |
| 273 | |
| 274 | // Get the target. |
| 275 | const Target *TheTarget = |
| 276 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
| 277 | if (!TheTarget) |
| 278 | return error(ErrorStr, Context); |
| 279 | std::string TripleName = TheTriple.getTriple(); |
| 280 | |
| 281 | // Create all the MC Objects. |
| 282 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 283 | if (!MRI) |
| 284 | return error(Twine("no register info for target ") + TripleName, Context); |
| 285 | |
| 286 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 287 | if (!MAI) |
| 288 | return error("no asm info for target " + TripleName, Context); |
| 289 | |
| 290 | MCObjectFileInfo MOFI; |
| 291 | MCContext MC(MAI.get(), MRI.get(), &MOFI); |
| 292 | MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, |
| 293 | MC); |
| 294 | |
| 295 | auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); |
| 296 | if (!MAB) |
| 297 | return error("no asm backend for target " + TripleName, Context); |
| 298 | |
| 299 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 300 | if (!MII) |
| 301 | return error("no instr info info for target " + TripleName, Context); |
| 302 | |
| 303 | std::unique_ptr<MCSubtargetInfo> MSTI( |
| 304 | TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 305 | if (!MSTI) |
| 306 | return error("no subtarget info for target " + TripleName, Context); |
| 307 | |
| 308 | MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC); |
| 309 | if (!MCE) |
| 310 | return error("no code emitter for target " + TripleName, Context); |
| 311 | |
| 312 | // Create the output file. |
| 313 | std::error_code EC; |
| 314 | raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None); |
| 315 | if (EC) |
| 316 | return error(Twine(OutputFilename) + ": " + EC.message(), Context); |
| 317 | |
| 318 | std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( |
| 319 | TheTriple, MC, *MAB, OutFile, MCE, *MSTI, false, |
| 320 | /*DWARFMustBeAtTheEnd*/ false)); |
| 321 | if (!MS) |
| 322 | return error("no object streamer for target " + TripleName, Context); |
| 323 | |
| 324 | if (auto Err = write(*MS, InputFiles)) |
| 325 | return error(Err.message(), "Writing DWP file"); |
| 326 | |
| 327 | MS->Finish(); |
David Blaikie | df05525 | 2015-12-01 00:48:34 +0000 | [diff] [blame] | 328 | } |