blob: e6a90cf8a3cf156baa29ff6aac4ca6b10aaed5ff [file] [log] [blame]
David Blaikie242b9482015-12-01 00:48:39 +00001#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 Blaikie98ad82a2015-12-01 18:07:07 +000012#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000013#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 Blaikieb073cb92015-12-02 06:21:34 +000020#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +000021#include <memory>
22#include <list>
23#include <unordered_set>
24
25using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000026using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000027using namespace cl;
28
29OptionCategory DwpCategory("Specific Options");
30static list<std::string> InputFiles(Positional, OneOrMore,
31 desc("<input files>"), cat(DwpCategory));
32
33static opt<std::string> OutputFilename(Required, "o", desc("Specify the output file."),
34 value_desc("filename"), cat(DwpCategory));
35
36static 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 Blaikie98ad82a2015-12-01 18:07:07 +000042static std::error_code
43writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000044 uint32_t &StringOffset, MCSection *StrSection,
45 MCSection *StrOffsetSection, StringRef CurStrSection,
46 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000047 // 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 Blaikiebb94e442015-12-01 19:17:58 +000059 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 Blaikie98ad82a2015-12-01 18:07:07 +000067 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 Blaikie242b9482015-12-01 00:48:39 +000082 return std::error_code();
83}
84
85static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +000086 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
87 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
88 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikieb073cb92015-12-02 06:21:34 +000089 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 Blaikie98ad82a2015-12-01 18:07:07 +0000103
104 StringMap<uint32_t> Strings;
105 uint32_t StringOffset = 0;
106
David Blaikieb073cb92015-12-02 06:21:34 +0000107 uint64_t UnitIndex = 0;
108 uint32_t ContributionOffsets[8] = {};
109
David Blaikie242b9482015-12-01 00:48:39 +0000110 for (const auto &Input : Inputs) {
111 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
112 if (!ErrOrObj)
113 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000114
115 IndexEntries.emplace_back();
116 UnitIndexEntry &CurEntry = IndexEntries.back();
117 CurEntry.Signature = UnitIndex++;
118
David Blaikie98ad82a2015-12-01 18:07:07 +0000119 StringRef CurStrSection;
120 StringRef CurStrOffsetSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000121
122 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie242b9482015-12-01 00:48:39 +0000123 StringRef Name;
124 if (std::error_code Err = Section.getName(Name))
125 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000126
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 Blaikie98ad82a2015-12-01 18:07:07 +0000151 }
David Blaikie242b9482015-12-01 00:48:39 +0000152 }
David Blaikieb073cb92015-12-02 06:21:34 +0000153
David Blaikiebb94e442015-12-01 19:17:58 +0000154 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
155 StrSection, StrOffsetSection,
156 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000157 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000158 }
David Blaikieb073cb92015-12-02 06:21:34 +0000159
David Blaikieb3757c02015-12-02 22:01:56 +0000160 unsigned Columns = 0;
161 for (auto &C : ContributionOffsets)
162 if (C)
163 ++Columns;
164
David Blaikieb073cb92015-12-02 06:21:34 +0000165 Out.SwitchSection(MCOFI.getDwarfCUIndexSection());
166 Out.EmitIntValue(2, 4); // Version
David Blaikieb3757c02015-12-02 22:01:56 +0000167 Out.EmitIntValue(Columns, 4); // Columns
David Blaikieb073cb92015-12-02 06:21:34 +0000168 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 Blaikieb3757c02015-12-02 22:01:56 +0000181 for (size_t i = 0; i != array_lengthof(ContributionOffsets); ++i)
182 if (ContributionOffsets[i])
183 Out.EmitIntValue(i + DW_SECT_INFO, 4);
David Blaikieb073cb92015-12-02 06:21:34 +0000184
185 // Write the offsets.
186 for (const auto &E : IndexEntries)
David Blaikieb3757c02015-12-02 22:01:56 +0000187 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
188 if (ContributionOffsets[i])
189 Out.EmitIntValue(E.Contributions[i].Offset, 4);
David Blaikieb073cb92015-12-02 06:21:34 +0000190
191 // Write the lengths.
192 for (const auto &E : IndexEntries)
David Blaikieb3757c02015-12-02 22:01:56 +0000193 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
194 if (ContributionOffsets[i])
195 Out.EmitIntValue(E.Contributions[i].Length, 4);
David Blaikieb073cb92015-12-02 06:21:34 +0000196
David Blaikie242b9482015-12-01 00:48:39 +0000197 return std::error_code();
198}
199
200int 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 Blaikiedf055252015-12-01 00:48:34 +0000268}