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