blob: 570854b849c17ff658a36d58aa4ac422b570921b [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"
David Majnemer03e2cc32015-12-21 22:09:27 +000013#include "llvm/MC/MCTargetOptionsCommandFlags.h"
David Blaikie242b9482015-12-01 00:48:39 +000014#include "llvm/Object/ObjectFile.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000015#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000016#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000017#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000018#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000019#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000020#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000021#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000022#include "llvm/Support/raw_ostream.h"
23#include "llvm/Target/TargetMachine.h"
David Blaikie242b9482015-12-01 00:48:39 +000024#include <list>
David Blaikie2ed678c2015-12-05 03:06:30 +000025#include <memory>
David Blaikie242b9482015-12-01 00:48:39 +000026#include <unordered_set>
27
28using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000029using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000030using namespace cl;
31
32OptionCategory DwpCategory("Specific Options");
33static list<std::string> InputFiles(Positional, OneOrMore,
34 desc("<input files>"), cat(DwpCategory));
35
David Blaikie2ed678c2015-12-05 03:06:30 +000036static opt<std::string> OutputFilename(Required, "o",
37 desc("Specify the output file."),
38 value_desc("filename"),
39 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000040
41static 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
David Blaikie98ad82a2015-12-01 18:07:07 +000047static std::error_code
48writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000049 uint32_t &StringOffset, MCSection *StrSection,
50 MCSection *StrOffsetSection, StringRef CurStrSection,
51 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000052 // 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);
David Blaikiebb94e442015-12-01 19:17:58 +000064 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;
David Blaikie98ad82a2015-12-01 18:07:07 +000072 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
David Blaikie242b9482015-12-01 00:48:39 +000087 return std::error_code();
88}
89
David Blaikiead07b5d2015-12-04 17:20:04 +000090static 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
106static 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
David Blaikie24c8ac92015-12-05 03:05:45 +0000136struct UnitIndexEntry {
137 uint64_t Signature;
138 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
139};
140
David Blaikiec3826da2015-12-09 21:02:33 +0000141static 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);
David Blaikie24c8ac92015-12-05 03:05:45 +0000149 uint32_t Offset = 0;
150 DataExtractor Data(Types, true, 0);
151 while (Data.isValidOffset(Offset)) {
David Blaikief5cb6272015-12-14 07:42:00 +0000152 UnitIndexEntry Entry = CUEntry;
David Blaikie24c8ac92015-12-05 03:05:45 +0000153 // Zero out the debug_info contribution
154 Entry.Contributions[0] = {};
155 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikief5cb6272015-12-14 07:42:00 +0000156 C.Offset = TypesOffset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000157 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;
David Blaikief5cb6272015-12-14 07:42:00 +0000166
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);
David Blaikie24c8ac92015-12-05 03:05:45 +0000176 }
177}
178
179static void
180writeIndexTable(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
189static 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;
David Blaikiec3826da2015-12-09 21:02:33 +0000202 while (Buckets[H]) {
203 assert(S != IndexEntries[Buckets[H] - 1].Signature &&
204 "Duplicate type unit");
David Blaikie24c8ac92015-12-05 03:05:45 +0000205 H += ((S >> 32) & Mask) | 1;
David Blaikiec3826da2015-12-09 21:02:33 +0000206 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000207 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
David Blaikie2ed678c2015-12-05 03:06:30 +0000214 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000215
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}
David Blaikie242b9482015-12-01 00:48:39 +0000237static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000238 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
239 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
240 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000241 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000242 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}},
David Blaikieb7020252015-12-04 21:16:42 +0000248 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikieb073cb92015-12-02 06:21:34 +0000249 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}};
250
David Blaikieb073cb92015-12-02 06:21:34 +0000251 std::vector<UnitIndexEntry> IndexEntries;
David Blaikie24c8ac92015-12-05 03:05:45 +0000252 std::vector<UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000253
254 StringMap<uint32_t> Strings;
255 uint32_t StringOffset = 0;
256
David Blaikieb073cb92015-12-02 06:21:34 +0000257 uint32_t ContributionOffsets[8] = {};
258
David Blaikie242b9482015-12-01 00:48:39 +0000259 for (const auto &Input : Inputs) {
260 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
261 if (!ErrOrObj)
262 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000263
264 IndexEntries.emplace_back();
265 UnitIndexEntry &CurEntry = IndexEntries.back();
David Blaikieb073cb92015-12-02 06:21:34 +0000266
David Blaikie98ad82a2015-12-01 18:07:07 +0000267 StringRef CurStrSection;
268 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000269 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000270 StringRef InfoSection;
271 StringRef AbbrevSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000272
273 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie242b9482015-12-01 00:48:39 +0000274 StringRef Name;
275 if (std::error_code Err = Section.getName(Name))
276 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000277
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;
David Blaikiec3826da2015-12-09 21:02:33 +0000289 if (Kind != DW_SECT_TYPES) {
290 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
291 ContributionOffsets[Index] +=
292 (CurEntry.Contributions[Index].Length = Contents.size());
293 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000294
David Blaikie24c8ac92015-12-05 03:05:45 +0000295 switch (Kind) {
296 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000297 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000298 break;
299 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000300 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000301 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000302 default:
303 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000304 }
David Blaikieb073cb92015-12-02 06:21:34 +0000305 }
306
307 MCSection *OutSection = SectionPair->second.first;
308 if (OutSection == StrOffsetSection)
309 CurStrOffsetSection = Contents;
310 else if (OutSection == StrSection)
311 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000312 else if (OutSection == TypesSection)
313 CurTypesSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000314 else {
315 Out.SwitchSection(OutSection);
316 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000317 }
David Blaikie242b9482015-12-01 00:48:39 +0000318 }
David Blaikieb073cb92015-12-02 06:21:34 +0000319
David Blaikiead07b5d2015-12-04 17:20:04 +0000320 assert(!AbbrevSection.empty());
321 assert(!InfoSection.empty());
322 CurEntry.Signature = getCUSignature(AbbrevSection, InfoSection);
David Blaikiec3826da2015-12-09 21:02:33 +0000323 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, CurEntry,
324 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikiead07b5d2015-12-04 17:20:04 +0000325
David Blaikiebb94e442015-12-01 19:17:58 +0000326 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
327 StrSection, StrOffsetSection,
328 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000329 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000330 }
David Blaikieb073cb92015-12-02 06:21:34 +0000331
David Blaikie9e51c842015-12-05 03:41:53 +0000332 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 }
David Blaikieb3757c02015-12-02 22:01:56 +0000339
David Blaikie24c8ac92015-12-05 03:05:45 +0000340 // Lie about the type contribution
341 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
342 // Unlie about the info contribution
343 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000344
David Blaikie24c8ac92015-12-05 03:05:45 +0000345 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
346 IndexEntries);
David Blaikieb073cb92015-12-02 06:21:34 +0000347
David Blaikie242b9482015-12-01 00:48:39 +0000348 return std::error_code();
349}
350
David Blaikie2ed678c2015-12-05 03:06:30 +0000351int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000352
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);
David Blaikie2ed678c2015-12-05 03:06:30 +0000383 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000384
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
David Majnemer03e2cc32015-12-21 22:09:27 +0000408 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000409 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000410 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
411 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000412 /*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();
David Blaikiedf055252015-12-01 00:48:34 +0000420}