blob: 9a9440574e8578049d3210634bacc53fdcedcbaf [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"
David Blaikie2ed678c2015-12-05 03:06:30 +00004#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
5#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +00006#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/Object/ObjectFile.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000014#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000015#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000016#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000017#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000018#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000019#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000020#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000021#include "llvm/Support/raw_ostream.h"
22#include "llvm/Target/TargetMachine.h"
David Blaikie242b9482015-12-01 00:48:39 +000023#include <list>
David Blaikie2ed678c2015-12-05 03:06:30 +000024#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000025#include <unordered_set>
26
27using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000028using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000029using namespace cl;
30
31OptionCategory DwpCategory("Specific Options");
32static list<std::string> InputFiles(Positional, OneOrMore,
33 desc("<input files>"), cat(DwpCategory));
34
David Blaikie2ed678c2015-12-05 03:06:30 +000035static opt<std::string> OutputFilename(Required, "o",
36 desc("Specify the output file."),
37 value_desc("filename"),
38 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000039
40static int error(const Twine &Error, const Twine &Context) {
41 errs() << Twine("while processing ") + Context + ":\n";
42 errs() << Twine("error: ") + Error + "\n";
43 return 1;
44}
45
David Blaikie98ad82a2015-12-01 18:07:07 +000046static std::error_code
47writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000048 uint32_t &StringOffset, MCSection *StrSection,
49 MCSection *StrOffsetSection, StringRef CurStrSection,
50 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000051 // Could possibly produce an error or warning if one of these was non-null but
52 // the other was null.
53 if (CurStrSection.empty() || CurStrOffsetSection.empty())
54 return std::error_code();
55
56 DenseMap<uint32_t, uint32_t> OffsetRemapping;
57
58 DataExtractor Data(CurStrSection, true, 0);
59 uint32_t LocalOffset = 0;
60 uint32_t PrevOffset = 0;
61 while (const char *s = Data.getCStr(&LocalOffset)) {
62 StringRef Str(s, LocalOffset - PrevOffset - 1);
David Blaikiebb94e442015-12-01 19:17:58 +000063 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
64 if (Pair.second) {
65 Out.SwitchSection(StrSection);
66 Out.EmitBytes(
67 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
68 StringOffset += Str.size() + 1;
69 }
70 OffsetRemapping[PrevOffset] = Pair.first->second;
David Blaikie98ad82a2015-12-01 18:07:07 +000071 PrevOffset = LocalOffset;
72 }
73
74 Data = DataExtractor(CurStrOffsetSection, true, 0);
75
76 Out.SwitchSection(StrOffsetSection);
77
78 uint32_t Offset = 0;
79 uint64_t Size = CurStrOffsetSection.size();
80 while (Offset < Size) {
81 auto OldOffset = Data.getU32(&Offset);
82 auto NewOffset = OffsetRemapping[OldOffset];
83 Out.EmitIntValue(NewOffset, 4);
84 }
85
David Blaikie242b9482015-12-01 00:48:39 +000086 return std::error_code();
87}
88
David Blaikiead07b5d2015-12-04 17:20:04 +000089static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
90 uint64_t CurCode;
91 uint32_t Offset = 0;
92 DataExtractor AbbrevData(Abbrev, true, 0);
93 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
94 // Tag
95 AbbrevData.getULEB128(&Offset);
96 // DW_CHILDREN
97 AbbrevData.getU8(&Offset);
98 // Attributes
99 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
100 ;
101 }
102 return Offset;
103}
104
105static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) {
106 uint32_t Offset = 0;
107 DataExtractor InfoData(Info, true, 0);
108 InfoData.getU32(&Offset); // Length
109 uint16_t Version = InfoData.getU16(&Offset);
110 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
111 uint8_t AddrSize = InfoData.getU8(&Offset);
112
113 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
114
115 DataExtractor AbbrevData(Abbrev, true, 0);
116 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
117 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
118 (void)Tag;
119 // FIXME: Real error handling
120 assert(Tag == dwarf::DW_TAG_compile_unit);
121 // DW_CHILDREN
122 AbbrevData.getU8(&AbbrevOffset);
123 uint32_t Name;
124 uint32_t Form;
125 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
126 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
127 Name != dwarf::DW_AT_GNU_dwo_id) {
128 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
129 }
130 // FIXME: Real error handling
131 assert(Name == dwarf::DW_AT_GNU_dwo_id);
132 return InfoData.getU64(&Offset);
133}
134
David Blaikie24c8ac92015-12-05 03:05:45 +0000135struct UnitIndexEntry {
136 uint64_t Signature;
137 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
138};
139
140static void addAllTypes(std::vector<UnitIndexEntry> &TypeIndexEntries,
141 uint32_t OutTypesOffset, StringRef Types,
142 const UnitIndexEntry &CUEntry) {
143 uint32_t Offset = 0;
144 DataExtractor Data(Types, true, 0);
145 while (Data.isValidOffset(Offset)) {
146 TypeIndexEntries.push_back(CUEntry);
147 auto &Entry = TypeIndexEntries.back();
148 // Zero out the debug_info contribution
149 Entry.Contributions[0] = {};
150 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
151 C.Offset = OutTypesOffset + Offset;
152 auto PrevOffset = Offset;
153 // Length of the unit, including the 4 byte length field.
154 C.Length = Data.getU32(&Offset) + 4;
155
156 Data.getU16(&Offset); // Version
157 Data.getU32(&Offset); // Abbrev offset
158 Data.getU8(&Offset); // Address size
159 Entry.Signature = Data.getU64(&Offset);
160 Offset = PrevOffset + C.Length;
161 }
162}
163
164static void
165writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
166 ArrayRef<UnitIndexEntry> IndexEntries,
167 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
168 for (const auto &E : IndexEntries)
169 for (size_t i = 0; i != array_lengthof(E.Contributions); ++i)
170 if (ContributionOffsets[i])
171 Out.EmitIntValue(E.Contributions[i].*Field, 4);
172}
173
174static void writeIndex(MCStreamer &Out, MCSection *Section,
175 ArrayRef<unsigned> ContributionOffsets,
176 ArrayRef<UnitIndexEntry> IndexEntries) {
177 unsigned Columns = 0;
178 for (auto &C : ContributionOffsets)
179 if (C)
180 ++Columns;
181
182 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
183 uint64_t Mask = Buckets.size() - 1;
184 for (size_t i = 0; i != IndexEntries.size(); ++i) {
185 auto S = IndexEntries[i].Signature;
186 auto H = S & Mask;
187 while (Buckets[H])
188 H += ((S >> 32) & Mask) | 1;
189 Buckets[H] = i + 1;
190 }
191
192 Out.SwitchSection(Section);
193 Out.EmitIntValue(2, 4); // Version
194 Out.EmitIntValue(Columns, 4); // Columns
195 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000196 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000197
198 // Write the signatures.
199 for (const auto &I : Buckets)
200 Out.EmitIntValue(I ? IndexEntries[I - 1].Signature : 0, 8);
201
202 // Write the indexes.
203 for (const auto &I : Buckets)
204 Out.EmitIntValue(I, 4);
205
206 // Write the column headers (which sections will appear in the table)
207 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
208 if (ContributionOffsets[i])
209 Out.EmitIntValue(i + DW_SECT_INFO, 4);
210
211 // Write the offsets.
212 writeIndexTable(Out, ContributionOffsets, IndexEntries,
213 &DWARFUnitIndex::Entry::SectionContribution::Offset);
214
215 // Write the lengths.
216 writeIndexTable(Out, ContributionOffsets, IndexEntries,
217 &DWARFUnitIndex::Entry::SectionContribution::Length);
218}
David Blaikie242b9482015-12-01 00:48:39 +0000219static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000220 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
221 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
222 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000223 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
224 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
225 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
226 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
227 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
228 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000229 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikieb073cb92015-12-02 06:21:34 +0000230 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}};
231
David Blaikieb073cb92015-12-02 06:21:34 +0000232 std::vector<UnitIndexEntry> IndexEntries;
David Blaikie24c8ac92015-12-05 03:05:45 +0000233 std::vector<UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000234
235 StringMap<uint32_t> Strings;
236 uint32_t StringOffset = 0;
237
David Blaikieb073cb92015-12-02 06:21:34 +0000238 uint32_t ContributionOffsets[8] = {};
239
David Blaikie242b9482015-12-01 00:48:39 +0000240 for (const auto &Input : Inputs) {
241 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
242 if (!ErrOrObj)
243 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000244
245 IndexEntries.emplace_back();
246 UnitIndexEntry &CurEntry = IndexEntries.back();
David Blaikieb073cb92015-12-02 06:21:34 +0000247
David Blaikie98ad82a2015-12-01 18:07:07 +0000248 StringRef CurStrSection;
249 StringRef CurStrOffsetSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000250 StringRef InfoSection;
251 StringRef AbbrevSection;
David Blaikie24c8ac92015-12-05 03:05:45 +0000252 StringRef TypesSection;
253
254 auto TypesOffset = ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikieb073cb92015-12-02 06:21:34 +0000255
256 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie242b9482015-12-01 00:48:39 +0000257 StringRef Name;
258 if (std::error_code Err = Section.getName(Name))
259 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000260
261 auto SectionPair =
262 KnownSections.find(Name.substr(Name.find_first_not_of("._")));
263 if (SectionPair == KnownSections.end())
264 continue;
265
266 StringRef Contents;
267 if (auto Err = Section.getContents(Contents))
268 return Err;
269
270 if (DWARFSectionKind Kind = SectionPair->second.second) {
271 auto Index = Kind - DW_SECT_INFO;
272 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
273 ContributionOffsets[Index] +=
274 (CurEntry.Contributions[Index].Length = Contents.size());
David Blaikiead07b5d2015-12-04 17:20:04 +0000275
David Blaikie24c8ac92015-12-05 03:05:45 +0000276 switch (Kind) {
277 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000278 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000279 break;
280 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000281 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000282 break;
283 case DW_SECT_TYPES:
284 TypesSection = Contents;
285 break;
286 default:
287 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000288 }
David Blaikieb073cb92015-12-02 06:21:34 +0000289 }
290
291 MCSection *OutSection = SectionPair->second.first;
292 if (OutSection == StrOffsetSection)
293 CurStrOffsetSection = Contents;
294 else if (OutSection == StrSection)
295 CurStrSection = Contents;
296 else {
297 Out.SwitchSection(OutSection);
298 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000299 }
David Blaikie242b9482015-12-01 00:48:39 +0000300 }
David Blaikieb073cb92015-12-02 06:21:34 +0000301
David Blaikiead07b5d2015-12-04 17:20:04 +0000302 assert(!AbbrevSection.empty());
303 assert(!InfoSection.empty());
304 CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection);
David Blaikie24c8ac92015-12-05 03:05:45 +0000305 addAllTypes(TypeIndexEntries, TypesOffset, TypesSection, CurEntry);
David Blaikiead07b5d2015-12-04 17:20:04 +0000306
David Blaikiebb94e442015-12-01 19:17:58 +0000307 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
308 StrSection, StrOffsetSection,
309 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000310 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000311 }
David Blaikieb073cb92015-12-02 06:21:34 +0000312
David Blaikie9e51c842015-12-05 03:41:53 +0000313 if (!TypeIndexEntries.empty()) {
314 // Lie about there being no info contributions so the TU index only includes
315 // the type unit contribution
316 ContributionOffsets[0] = 0;
317 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
318 TypeIndexEntries);
319 }
David Blaikieb3757c02015-12-02 22:01:56 +0000320
David Blaikie24c8ac92015-12-05 03:05:45 +0000321 // Lie about the type contribution
322 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
323 // Unlie about the info contribution
324 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000325
David Blaikie24c8ac92015-12-05 03:05:45 +0000326 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
327 IndexEntries);
David Blaikieb073cb92015-12-02 06:21:34 +0000328
David Blaikie242b9482015-12-01 00:48:39 +0000329 return std::error_code();
330}
331
David Blaikie2ed678c2015-12-05 03:06:30 +0000332int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000333
334 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
335
336 llvm::InitializeAllTargetInfos();
337 llvm::InitializeAllTargetMCs();
338 llvm::InitializeAllTargets();
339 llvm::InitializeAllAsmPrinters();
340
341 std::string ErrorStr;
342 StringRef Context = "dwarf streamer init";
343
344 Triple TheTriple("x86_64-linux-gnu");
345
346 // Get the target.
347 const Target *TheTarget =
348 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
349 if (!TheTarget)
350 return error(ErrorStr, Context);
351 std::string TripleName = TheTriple.getTriple();
352
353 // Create all the MC Objects.
354 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
355 if (!MRI)
356 return error(Twine("no register info for target ") + TripleName, Context);
357
358 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
359 if (!MAI)
360 return error("no asm info for target " + TripleName, Context);
361
362 MCObjectFileInfo MOFI;
363 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000364 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000365
366 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
367 if (!MAB)
368 return error("no asm backend for target " + TripleName, Context);
369
370 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
371 if (!MII)
372 return error("no instr info info for target " + TripleName, Context);
373
374 std::unique_ptr<MCSubtargetInfo> MSTI(
375 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
376 if (!MSTI)
377 return error("no subtarget info for target " + TripleName, Context);
378
379 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
380 if (!MCE)
381 return error("no code emitter for target " + TripleName, Context);
382
383 // Create the output file.
384 std::error_code EC;
385 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
386 if (EC)
387 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
388
389 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
390 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, false,
391 /*DWARFMustBeAtTheEnd*/ false));
392 if (!MS)
393 return error("no object streamer for target " + TripleName, Context);
394
395 if (auto Err = write(*MS, InputFiles))
396 return error(Err.message(), "Writing DWP file");
397
398 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000399}