blob: ca40638d80136db27e42ad6aec2f0d07abc8b163 [file] [log] [blame]
David Blaikie852c02b2016-02-19 21:09:26 +00001#include "llvm/ADT/MapVector.h"
David Blaikie242b9482015-12-01 00:48:39 +00002#include "llvm/ADT/STLExtras.h"
3#include "llvm/ADT/StringSet.h"
4#include "llvm/CodeGen/AsmPrinter.h"
David Blaikie2ed678c2015-12-05 03:06:30 +00005#include "llvm/DebugInfo/DWARF/DWARFFormValue.h"
6#include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
David Blaikie242b9482015-12-01 00:48:39 +00007#include "llvm/MC/MCAsmInfo.h"
8#include "llvm/MC/MCContext.h"
9#include "llvm/MC/MCInstrInfo.h"
10#include "llvm/MC/MCObjectFileInfo.h"
11#include "llvm/MC/MCRegisterInfo.h"
12#include "llvm/MC/MCSectionELF.h"
13#include "llvm/MC/MCStreamer.h"
David Majnemer03e2cc32015-12-21 22:09:27 +000014#include "llvm/MC/MCTargetOptionsCommandFlags.h"
David Blaikie242b9482015-12-01 00:48:39 +000015#include "llvm/Object/ObjectFile.h"
David Blaikie74f5b282016-02-19 01:51:44 +000016#include "llvm/Support/Compression.h"
David Blaikie98ad82a2015-12-01 18:07:07 +000017#include "llvm/Support/DataExtractor.h"
David Blaikie242b9482015-12-01 00:48:39 +000018#include "llvm/Support/FileSystem.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000019#include "llvm/Support/MathExtras.h"
David Blaikie242b9482015-12-01 00:48:39 +000020#include "llvm/Support/MemoryBuffer.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000021#include "llvm/Support/Options.h"
David Blaikie242b9482015-12-01 00:48:39 +000022#include "llvm/Support/TargetRegistry.h"
David Blaikie2ed678c2015-12-05 03:06:30 +000023#include "llvm/Support/TargetSelect.h"
David Blaikie242b9482015-12-01 00:48:39 +000024#include "llvm/Support/raw_ostream.h"
25#include "llvm/Target/TargetMachine.h"
David Blaikie242b9482015-12-01 00:48:39 +000026#include <list>
David Blaikie2ed678c2015-12-05 03:06:30 +000027#include <memory>
David Blaikie23919372016-02-06 01:15:26 +000028#include <system_error>
David Blaikie852c02b2016-02-19 21:09:26 +000029#include <unordered_set>
David Blaikie242b9482015-12-01 00:48:39 +000030
31using namespace llvm;
David Blaikie98ad82a2015-12-01 18:07:07 +000032using namespace llvm::object;
David Blaikie242b9482015-12-01 00:48:39 +000033using namespace cl;
34
35OptionCategory DwpCategory("Specific Options");
36static list<std::string> InputFiles(Positional, OneOrMore,
37 desc("<input files>"), cat(DwpCategory));
38
David Blaikie2ed678c2015-12-05 03:06:30 +000039static opt<std::string> OutputFilename(Required, "o",
40 desc("Specify the output file."),
41 value_desc("filename"),
42 cat(DwpCategory));
David Blaikie242b9482015-12-01 00:48:39 +000043
44static int error(const Twine &Error, const Twine &Context) {
45 errs() << Twine("while processing ") + Context + ":\n";
46 errs() << Twine("error: ") + Error + "\n";
47 return 1;
48}
49
David Blaikie98ad82a2015-12-01 18:07:07 +000050static std::error_code
51writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings,
David Blaikiebb94e442015-12-01 19:17:58 +000052 uint32_t &StringOffset, MCSection *StrSection,
53 MCSection *StrOffsetSection, StringRef CurStrSection,
54 StringRef CurStrOffsetSection) {
David Blaikie98ad82a2015-12-01 18:07:07 +000055 // Could possibly produce an error or warning if one of these was non-null but
56 // the other was null.
57 if (CurStrSection.empty() || CurStrOffsetSection.empty())
58 return std::error_code();
59
60 DenseMap<uint32_t, uint32_t> OffsetRemapping;
61
62 DataExtractor Data(CurStrSection, true, 0);
63 uint32_t LocalOffset = 0;
64 uint32_t PrevOffset = 0;
65 while (const char *s = Data.getCStr(&LocalOffset)) {
66 StringRef Str(s, LocalOffset - PrevOffset - 1);
David Blaikiebb94e442015-12-01 19:17:58 +000067 auto Pair = Strings.insert(std::make_pair(Str, StringOffset));
68 if (Pair.second) {
69 Out.SwitchSection(StrSection);
70 Out.EmitBytes(
71 StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1));
72 StringOffset += Str.size() + 1;
73 }
74 OffsetRemapping[PrevOffset] = Pair.first->second;
David Blaikie98ad82a2015-12-01 18:07:07 +000075 PrevOffset = LocalOffset;
76 }
77
78 Data = DataExtractor(CurStrOffsetSection, true, 0);
79
80 Out.SwitchSection(StrOffsetSection);
81
82 uint32_t Offset = 0;
83 uint64_t Size = CurStrOffsetSection.size();
84 while (Offset < Size) {
85 auto OldOffset = Data.getU32(&Offset);
86 auto NewOffset = OffsetRemapping[OldOffset];
87 Out.EmitIntValue(NewOffset, 4);
88 }
89
David Blaikie242b9482015-12-01 00:48:39 +000090 return std::error_code();
91}
92
David Blaikiead07b5d2015-12-04 17:20:04 +000093static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) {
94 uint64_t CurCode;
95 uint32_t Offset = 0;
96 DataExtractor AbbrevData(Abbrev, true, 0);
97 while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) {
98 // Tag
99 AbbrevData.getULEB128(&Offset);
100 // DW_CHILDREN
101 AbbrevData.getU8(&Offset);
102 // Attributes
103 while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset))
104 ;
105 }
106 return Offset;
107}
108
109static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) {
110 uint32_t Offset = 0;
111 DataExtractor InfoData(Info, true, 0);
112 InfoData.getU32(&Offset); // Length
113 uint16_t Version = InfoData.getU16(&Offset);
114 InfoData.getU32(&Offset); // Abbrev offset (should be zero)
115 uint8_t AddrSize = InfoData.getU8(&Offset);
116
117 uint32_t AbbrCode = InfoData.getULEB128(&Offset);
118
119 DataExtractor AbbrevData(Abbrev, true, 0);
120 uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode);
121 uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset);
122 (void)Tag;
123 // FIXME: Real error handling
124 assert(Tag == dwarf::DW_TAG_compile_unit);
125 // DW_CHILDREN
126 AbbrevData.getU8(&AbbrevOffset);
127 uint32_t Name;
128 uint32_t Form;
129 while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) |
130 (Form = AbbrevData.getULEB128(&AbbrevOffset)) &&
131 Name != dwarf::DW_AT_GNU_dwo_id) {
132 DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize);
133 }
134 // FIXME: Real error handling
135 assert(Name == dwarf::DW_AT_GNU_dwo_id);
136 return InfoData.getU64(&Offset);
137}
138
David Blaikie24c8ac92015-12-05 03:05:45 +0000139struct UnitIndexEntry {
David Blaikie24c8ac92015-12-05 03:05:45 +0000140 DWARFUnitIndex::Entry::SectionContribution Contributions[8];
141};
142
David Blaikie852c02b2016-02-19 21:09:26 +0000143static void addAllTypesFromDWP(
144 MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
145 const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types,
146 const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) {
David Blaikie8bce5a02016-02-17 07:00:24 +0000147 Out.SwitchSection(OutputTypes);
148 for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) {
149 auto *I = E.getOffsets();
150 if (!I)
151 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000152 auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry));
153 if (!P.second)
David Blaikie8bce5a02016-02-17 07:00:24 +0000154 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000155 auto &Entry = P.first->second;
David Blaikie8bce5a02016-02-17 07:00:24 +0000156 // Zero out the debug_info contribution
157 Entry.Contributions[0] = {};
158 for (auto Kind : TUIndex.getColumnKinds()) {
159 auto &C = Entry.Contributions[Kind - DW_SECT_INFO];
160 C.Offset += I->Offset;
161 C.Length = I->Length;
162 ++I;
163 }
164 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
165 Out.EmitBytes(Types.substr(
166 C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset,
167 C.Length));
168 C.Offset = TypesOffset;
169 TypesOffset += C.Length;
David Blaikie8bce5a02016-02-17 07:00:24 +0000170 }
171}
172
David Blaikiec3826da2015-12-09 21:02:33 +0000173static void addAllTypes(MCStreamer &Out,
David Blaikie852c02b2016-02-19 21:09:26 +0000174 MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries,
David Blaikiec3826da2015-12-09 21:02:33 +0000175 MCSection *OutputTypes, StringRef Types,
176 const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) {
177 if (Types.empty())
178 return;
179
180 Out.SwitchSection(OutputTypes);
David Blaikie24c8ac92015-12-05 03:05:45 +0000181 uint32_t Offset = 0;
182 DataExtractor Data(Types, true, 0);
183 while (Data.isValidOffset(Offset)) {
David Blaikief5cb6272015-12-14 07:42:00 +0000184 UnitIndexEntry Entry = CUEntry;
David Blaikie24c8ac92015-12-05 03:05:45 +0000185 // Zero out the debug_info contribution
186 Entry.Contributions[0] = {};
187 auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO];
David Blaikief5cb6272015-12-14 07:42:00 +0000188 C.Offset = TypesOffset;
David Blaikie24c8ac92015-12-05 03:05:45 +0000189 auto PrevOffset = Offset;
190 // Length of the unit, including the 4 byte length field.
191 C.Length = Data.getU32(&Offset) + 4;
192
193 Data.getU16(&Offset); // Version
194 Data.getU32(&Offset); // Abbrev offset
195 Data.getU8(&Offset); // Address size
David Blaikie852c02b2016-02-19 21:09:26 +0000196 auto Signature = Data.getU64(&Offset);
David Blaikie24c8ac92015-12-05 03:05:45 +0000197 Offset = PrevOffset + C.Length;
David Blaikief5cb6272015-12-14 07:42:00 +0000198
David Blaikie852c02b2016-02-19 21:09:26 +0000199 auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry));
200 if (!P.second)
David Blaikief5cb6272015-12-14 07:42:00 +0000201 continue;
202
203 Out.EmitBytes(Types.substr(PrevOffset, C.Length));
204 TypesOffset += C.Length;
David Blaikie24c8ac92015-12-05 03:05:45 +0000205 }
206}
207
208static void
209writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets,
David Blaikie852c02b2016-02-19 21:09:26 +0000210 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
David Blaikie24c8ac92015-12-05 03:05:45 +0000211 uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) {
212 for (const auto &E : IndexEntries)
David Blaikie852c02b2016-02-19 21:09:26 +0000213 for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i)
David Blaikie24c8ac92015-12-05 03:05:45 +0000214 if (ContributionOffsets[i])
David Blaikie852c02b2016-02-19 21:09:26 +0000215 Out.EmitIntValue(E.second.Contributions[i].*Field, 4);
David Blaikie24c8ac92015-12-05 03:05:45 +0000216}
217
David Blaikie852c02b2016-02-19 21:09:26 +0000218static void
219writeIndex(MCStreamer &Out, MCSection *Section,
220 ArrayRef<unsigned> ContributionOffsets,
221 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) {
David Blaikie24c8ac92015-12-05 03:05:45 +0000222 unsigned Columns = 0;
223 for (auto &C : ContributionOffsets)
224 if (C)
225 ++Columns;
226
227 std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2));
228 uint64_t Mask = Buckets.size() - 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000229 size_t i = 0;
230 for (const auto &P : IndexEntries) {
231 auto S = P.first;
David Blaikie24c8ac92015-12-05 03:05:45 +0000232 auto H = S & Mask;
David Blaikiec3826da2015-12-09 21:02:33 +0000233 while (Buckets[H]) {
David Blaikie852c02b2016-02-19 21:09:26 +0000234 assert(S != IndexEntries.begin()[Buckets[H] - 1].first &&
David Blaikie74f5b282016-02-19 01:51:44 +0000235 "Duplicate unit");
Benjamin Kramere5930942016-02-18 13:23:17 +0000236 H = (H + (((S >> 32) & Mask) | 1)) % Buckets.size();
David Blaikiec3826da2015-12-09 21:02:33 +0000237 }
David Blaikie24c8ac92015-12-05 03:05:45 +0000238 Buckets[H] = i + 1;
David Blaikie852c02b2016-02-19 21:09:26 +0000239 ++i;
David Blaikie24c8ac92015-12-05 03:05:45 +0000240 }
241
242 Out.SwitchSection(Section);
243 Out.EmitIntValue(2, 4); // Version
244 Out.EmitIntValue(Columns, 4); // Columns
245 Out.EmitIntValue(IndexEntries.size(), 4); // Num Units
David Blaikie2ed678c2015-12-05 03:06:30 +0000246 Out.EmitIntValue(Buckets.size(), 4); // Num Buckets
David Blaikie24c8ac92015-12-05 03:05:45 +0000247
248 // Write the signatures.
249 for (const auto &I : Buckets)
David Blaikie852c02b2016-02-19 21:09:26 +0000250 Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8);
David Blaikie24c8ac92015-12-05 03:05:45 +0000251
252 // Write the indexes.
253 for (const auto &I : Buckets)
254 Out.EmitIntValue(I, 4);
255
256 // Write the column headers (which sections will appear in the table)
257 for (size_t i = 0; i != ContributionOffsets.size(); ++i)
258 if (ContributionOffsets[i])
259 Out.EmitIntValue(i + DW_SECT_INFO, 4);
260
261 // Write the offsets.
262 writeIndexTable(Out, ContributionOffsets, IndexEntries,
263 &DWARFUnitIndex::Entry::SectionContribution::Offset);
264
265 // Write the lengths.
266 writeIndexTable(Out, ContributionOffsets, IndexEntries,
267 &DWARFUnitIndex::Entry::SectionContribution::Length);
268}
David Blaikie74f5b282016-02-19 01:51:44 +0000269static bool consumeCompressedDebugSectionHeader(StringRef &data,
270 uint64_t &OriginalSize) {
271 // Consume "ZLIB" prefix.
272 if (!data.startswith("ZLIB"))
273 return false;
274 data = data.substr(4);
275 // Consume uncompressed section size (big-endian 8 bytes).
276 DataExtractor extractor(data, false, 8);
277 uint32_t Offset = 0;
278 OriginalSize = extractor.getU64(&Offset);
279 if (Offset == 0)
280 return false;
281 data = data.substr(Offset);
282 return true;
283}
284
David Blaikie242b9482015-12-01 00:48:39 +0000285static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) {
David Blaikie98ad82a2015-12-01 18:07:07 +0000286 const auto &MCOFI = *Out.getContext().getObjectFileInfo();
287 MCSection *const StrSection = MCOFI.getDwarfStrDWOSection();
288 MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection();
David Blaikiec3826da2015-12-09 21:02:33 +0000289 MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection();
David Blaikie23919372016-02-06 01:15:26 +0000290 MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection();
David Blaikie8bce5a02016-02-17 07:00:24 +0000291 MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection();
David Blaikieb073cb92015-12-02 06:21:34 +0000292 const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = {
293 {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}},
294 {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}},
295 {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}},
296 {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}},
297 {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}},
David Blaikieb7020252015-12-04 21:16:42 +0000298 {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}},
David Blaikie23919372016-02-06 01:15:26 +0000299 {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}},
David Blaikie8bce5a02016-02-17 07:00:24 +0000300 {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}},
301 {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}};
David Blaikieb073cb92015-12-02 06:21:34 +0000302
David Blaikie852c02b2016-02-19 21:09:26 +0000303 MapVector<uint64_t, UnitIndexEntry> IndexEntries;
304 MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries;
David Blaikie98ad82a2015-12-01 18:07:07 +0000305
306 StringMap<uint32_t> Strings;
307 uint32_t StringOffset = 0;
308
David Blaikieb073cb92015-12-02 06:21:34 +0000309 uint32_t ContributionOffsets[8] = {};
310
David Blaikie242b9482015-12-01 00:48:39 +0000311 for (const auto &Input : Inputs) {
312 auto ErrOrObj = object::ObjectFile::createObjectFile(Input);
313 if (!ErrOrObj)
314 return ErrOrObj.getError();
David Blaikieb073cb92015-12-02 06:21:34 +0000315
David Blaikie23919372016-02-06 01:15:26 +0000316 UnitIndexEntry CurEntry = {};
David Blaikieb073cb92015-12-02 06:21:34 +0000317
David Blaikie98ad82a2015-12-01 18:07:07 +0000318 StringRef CurStrSection;
319 StringRef CurStrOffsetSection;
David Blaikiec3826da2015-12-09 21:02:33 +0000320 StringRef CurTypesSection;
David Blaikiead07b5d2015-12-04 17:20:04 +0000321 StringRef InfoSection;
322 StringRef AbbrevSection;
David Blaikie23919372016-02-06 01:15:26 +0000323 StringRef CurCUIndexSection;
David Blaikie8bce5a02016-02-17 07:00:24 +0000324 StringRef CurTUIndexSection;
David Blaikieb073cb92015-12-02 06:21:34 +0000325
David Blaikie74f5b282016-02-19 01:51:44 +0000326 SmallVector<SmallString<32>, 4> UncompressedSections;
327
David Blaikieb073cb92015-12-02 06:21:34 +0000328 for (const auto &Section : ErrOrObj->getBinary()->sections()) {
David Blaikie74f5b282016-02-19 01:51:44 +0000329 if (Section.isBSS())
330 continue;
331 if (Section.isVirtual())
332 continue;
333
David Blaikie242b9482015-12-01 00:48:39 +0000334 StringRef Name;
335 if (std::error_code Err = Section.getName(Name))
336 return Err;
David Blaikieb073cb92015-12-02 06:21:34 +0000337
David Blaikie74f5b282016-02-19 01:51:44 +0000338 Name = Name.substr(Name.find_first_not_of("._"));
David Blaikieb073cb92015-12-02 06:21:34 +0000339
340 StringRef Contents;
341 if (auto Err = Section.getContents(Contents))
342 return Err;
343
David Blaikie74f5b282016-02-19 01:51:44 +0000344 if (Name.startswith("zdebug_")) {
345 uint64_t OriginalSize;
346 if (!zlib::isAvailable() ||
347 !consumeCompressedDebugSectionHeader(Contents, OriginalSize))
David Blaikie9a5d3642016-02-19 02:03:45 +0000348 return make_error_code(std::errc::invalid_argument);
David Blaikie74f5b282016-02-19 01:51:44 +0000349 UncompressedSections.resize(UncompressedSections.size() + 1);
350 if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) !=
351 zlib::StatusOK) {
352 UncompressedSections.pop_back();
353 continue;
354 }
355 Name = Name.substr(1);
356 Contents = UncompressedSections.back();
357 }
358
359 auto SectionPair = KnownSections.find(Name);
360 if (SectionPair == KnownSections.end())
361 continue;
362
David Blaikieb073cb92015-12-02 06:21:34 +0000363 if (DWARFSectionKind Kind = SectionPair->second.second) {
364 auto Index = Kind - DW_SECT_INFO;
David Blaikiec3826da2015-12-09 21:02:33 +0000365 if (Kind != DW_SECT_TYPES) {
366 CurEntry.Contributions[Index].Offset = ContributionOffsets[Index];
367 ContributionOffsets[Index] +=
368 (CurEntry.Contributions[Index].Length = Contents.size());
369 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000370
David Blaikie24c8ac92015-12-05 03:05:45 +0000371 switch (Kind) {
372 case DW_SECT_INFO:
David Blaikiead07b5d2015-12-04 17:20:04 +0000373 InfoSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000374 break;
375 case DW_SECT_ABBREV:
David Blaikiead07b5d2015-12-04 17:20:04 +0000376 AbbrevSection = Contents;
David Blaikie24c8ac92015-12-05 03:05:45 +0000377 break;
David Blaikie24c8ac92015-12-05 03:05:45 +0000378 default:
379 break;
David Blaikiead07b5d2015-12-04 17:20:04 +0000380 }
David Blaikieb073cb92015-12-02 06:21:34 +0000381 }
382
383 MCSection *OutSection = SectionPair->second.first;
384 if (OutSection == StrOffsetSection)
385 CurStrOffsetSection = Contents;
386 else if (OutSection == StrSection)
387 CurStrSection = Contents;
David Blaikiec3826da2015-12-09 21:02:33 +0000388 else if (OutSection == TypesSection)
389 CurTypesSection = Contents;
David Blaikie23919372016-02-06 01:15:26 +0000390 else if (OutSection == CUIndexSection)
391 CurCUIndexSection = Contents;
David Blaikie8bce5a02016-02-17 07:00:24 +0000392 else if (OutSection == TUIndexSection)
393 CurTUIndexSection = Contents;
David Blaikieb073cb92015-12-02 06:21:34 +0000394 else {
395 Out.SwitchSection(OutSection);
396 Out.EmitBytes(Contents);
David Blaikie98ad82a2015-12-01 18:07:07 +0000397 }
David Blaikie242b9482015-12-01 00:48:39 +0000398 }
David Blaikieb073cb92015-12-02 06:21:34 +0000399
David Blaikiead07b5d2015-12-04 17:20:04 +0000400 assert(!AbbrevSection.empty());
401 assert(!InfoSection.empty());
David Blaikie23919372016-02-06 01:15:26 +0000402 if (!CurCUIndexSection.empty()) {
403 DWARFUnitIndex CUIndex(DW_SECT_INFO);
404 DataExtractor CUIndexData(CurCUIndexSection,
405 ErrOrObj->getBinary()->isLittleEndian(), 0);
406 if (!CUIndex.parse(CUIndexData))
407 return make_error_code(std::errc::invalid_argument);
408
409 for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) {
David Blaikie23919372016-02-06 01:15:26 +0000410 auto *I = E.getOffsets();
411 if (!I)
412 continue;
David Blaikie852c02b2016-02-19 21:09:26 +0000413 auto P =
414 IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry));
415 // FIXME: Check P.second and error for duplicate CU signatures
416 auto &NewEntry = P.first->second;
David Blaikie23919372016-02-06 01:15:26 +0000417 for (auto Kind : CUIndex.getColumnKinds()) {
418 auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO];
419 C.Offset += I->Offset;
420 C.Length = I->Length;
421 ++I;
422 }
David Blaikie23919372016-02-06 01:15:26 +0000423 }
David Blaikie8bce5a02016-02-17 07:00:24 +0000424
425 if (!CurTypesSection.empty()) {
426 if (CurTUIndexSection.empty())
427 return make_error_code(std::errc::invalid_argument);
428 DWARFUnitIndex TUIndex(DW_SECT_TYPES);
429 DataExtractor TUIndexData(CurTUIndexSection,
430 ErrOrObj->getBinary()->isLittleEndian(), 0);
431 if (!TUIndex.parse(TUIndexData))
432 return make_error_code(std::errc::invalid_argument);
433 addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection,
434 CurTypesSection, CurEntry,
435 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
436 }
David Blaikie23919372016-02-06 01:15:26 +0000437 } else {
David Blaikie852c02b2016-02-19 21:09:26 +0000438 IndexEntries.insert(
439 std::make_pair(getCUSignature(AbbrevSection, InfoSection), CurEntry));
440 // FIXME: Check P.second and error for duplicate CU signatures
David Blaikie23919372016-02-06 01:15:26 +0000441 addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection,
442 CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]);
David Blaikie23919372016-02-06 01:15:26 +0000443 }
David Blaikiead07b5d2015-12-04 17:20:04 +0000444
David Blaikiebb94e442015-12-01 19:17:58 +0000445 if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset,
446 StrSection, StrOffsetSection,
447 CurStrSection, CurStrOffsetSection))
David Blaikie98ad82a2015-12-01 18:07:07 +0000448 return Err;
David Blaikie242b9482015-12-01 00:48:39 +0000449 }
David Blaikieb073cb92015-12-02 06:21:34 +0000450
David Blaikie9e51c842015-12-05 03:41:53 +0000451 if (!TypeIndexEntries.empty()) {
452 // Lie about there being no info contributions so the TU index only includes
453 // the type unit contribution
454 ContributionOffsets[0] = 0;
455 writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets,
456 TypeIndexEntries);
457 }
David Blaikieb3757c02015-12-02 22:01:56 +0000458
David Blaikie24c8ac92015-12-05 03:05:45 +0000459 // Lie about the type contribution
460 ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0;
461 // Unlie about the info contribution
462 ContributionOffsets[0] = 1;
David Blaikie7c4ffe02015-12-04 21:30:23 +0000463
David Blaikie24c8ac92015-12-05 03:05:45 +0000464 writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets,
465 IndexEntries);
David Blaikieb073cb92015-12-02 06:21:34 +0000466
David Blaikie242b9482015-12-01 00:48:39 +0000467 return std::error_code();
468}
469
David Blaikie2ed678c2015-12-05 03:06:30 +0000470int main(int argc, char **argv) {
David Blaikie242b9482015-12-01 00:48:39 +0000471
472 ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files");
473
474 llvm::InitializeAllTargetInfos();
475 llvm::InitializeAllTargetMCs();
476 llvm::InitializeAllTargets();
477 llvm::InitializeAllAsmPrinters();
478
479 std::string ErrorStr;
480 StringRef Context = "dwarf streamer init";
481
482 Triple TheTriple("x86_64-linux-gnu");
483
484 // Get the target.
485 const Target *TheTarget =
486 TargetRegistry::lookupTarget("", TheTriple, ErrorStr);
487 if (!TheTarget)
488 return error(ErrorStr, Context);
489 std::string TripleName = TheTriple.getTriple();
490
491 // Create all the MC Objects.
492 std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName));
493 if (!MRI)
494 return error(Twine("no register info for target ") + TripleName, Context);
495
496 std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName));
497 if (!MAI)
498 return error("no asm info for target " + TripleName, Context);
499
500 MCObjectFileInfo MOFI;
501 MCContext MC(MAI.get(), MRI.get(), &MOFI);
David Blaikie2ed678c2015-12-05 03:06:30 +0000502 MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC);
David Blaikie242b9482015-12-01 00:48:39 +0000503
504 auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, "");
505 if (!MAB)
506 return error("no asm backend for target " + TripleName, Context);
507
508 std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo());
509 if (!MII)
510 return error("no instr info info for target " + TripleName, Context);
511
512 std::unique_ptr<MCSubtargetInfo> MSTI(
513 TheTarget->createMCSubtargetInfo(TripleName, "", ""));
514 if (!MSTI)
515 return error("no subtarget info for target " + TripleName, Context);
516
517 MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC);
518 if (!MCE)
519 return error("no code emitter for target " + TripleName, Context);
520
521 // Create the output file.
522 std::error_code EC;
523 raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None);
524 if (EC)
525 return error(Twine(OutputFilename) + ": " + EC.message(), Context);
526
David Majnemer03e2cc32015-12-21 22:09:27 +0000527 MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags();
David Blaikie242b9482015-12-01 00:48:39 +0000528 std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer(
David Majnemer03e2cc32015-12-21 22:09:27 +0000529 TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll,
530 MCOptions.MCIncrementalLinkerCompatible,
David Blaikie242b9482015-12-01 00:48:39 +0000531 /*DWARFMustBeAtTheEnd*/ false));
532 if (!MS)
533 return error("no object streamer for target " + TripleName, Context);
534
535 if (auto Err = write(*MS, InputFiles))
536 return error(Err.message(), "Writing DWP file");
537
538 MS->Finish();
David Blaikiedf055252015-12-01 00:48:34 +0000539}