David Blaikie | f72dbc1 | 2016-03-01 22:29:00 +0000 | [diff] [blame^] | 1 | //===-- llvm-dwp.cpp - Split DWARF merging tool for llvm ------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // A utility for merging DWARF 5 Split DWARF .dwo files into .dwp (DWARF |
| 11 | // package files). |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 14 | #include "llvm/ADT/MapVector.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/STLExtras.h" |
| 16 | #include "llvm/ADT/StringSet.h" |
| 17 | #include "llvm/CodeGen/AsmPrinter.h" |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 18 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 19 | #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 20 | #include "llvm/MC/MCAsmInfo.h" |
| 21 | #include "llvm/MC/MCContext.h" |
| 22 | #include "llvm/MC/MCInstrInfo.h" |
| 23 | #include "llvm/MC/MCObjectFileInfo.h" |
| 24 | #include "llvm/MC/MCRegisterInfo.h" |
| 25 | #include "llvm/MC/MCSectionELF.h" |
| 26 | #include "llvm/MC/MCStreamer.h" |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 27 | #include "llvm/MC/MCTargetOptionsCommandFlags.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 28 | #include "llvm/Object/ObjectFile.h" |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 29 | #include "llvm/Support/Compression.h" |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 30 | #include "llvm/Support/DataExtractor.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 31 | #include "llvm/Support/FileSystem.h" |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 32 | #include "llvm/Support/MathExtras.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 33 | #include "llvm/Support/MemoryBuffer.h" |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Options.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 35 | #include "llvm/Support/TargetRegistry.h" |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 36 | #include "llvm/Support/TargetSelect.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 37 | #include "llvm/Support/raw_ostream.h" |
| 38 | #include "llvm/Target/TargetMachine.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 39 | #include <list> |
David Blaikie | f1958da | 2016-02-26 07:30:15 +0000 | [diff] [blame] | 40 | #include <iostream> |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 41 | #include <memory> |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 42 | #include <system_error> |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 43 | #include <unordered_set> |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 44 | |
| 45 | using namespace llvm; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 46 | using namespace llvm::object; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 47 | using namespace cl; |
| 48 | |
| 49 | OptionCategory DwpCategory("Specific Options"); |
| 50 | static list<std::string> InputFiles(Positional, OneOrMore, |
| 51 | desc("<input files>"), cat(DwpCategory)); |
| 52 | |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 53 | static opt<std::string> OutputFilename(Required, "o", |
| 54 | desc("Specify the output file."), |
| 55 | value_desc("filename"), |
| 56 | cat(DwpCategory)); |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 57 | |
| 58 | static int error(const Twine &Error, const Twine &Context) { |
| 59 | errs() << Twine("while processing ") + Context + ":\n"; |
| 60 | errs() << Twine("error: ") + Error + "\n"; |
| 61 | return 1; |
| 62 | } |
| 63 | |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 64 | static std::error_code |
| 65 | writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings, |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 66 | uint32_t &StringOffset, MCSection *StrSection, |
| 67 | MCSection *StrOffsetSection, StringRef CurStrSection, |
| 68 | StringRef CurStrOffsetSection) { |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 69 | // Could possibly produce an error or warning if one of these was non-null but |
| 70 | // the other was null. |
| 71 | if (CurStrSection.empty() || CurStrOffsetSection.empty()) |
| 72 | return std::error_code(); |
| 73 | |
| 74 | DenseMap<uint32_t, uint32_t> OffsetRemapping; |
| 75 | |
| 76 | DataExtractor Data(CurStrSection, true, 0); |
| 77 | uint32_t LocalOffset = 0; |
| 78 | uint32_t PrevOffset = 0; |
| 79 | while (const char *s = Data.getCStr(&LocalOffset)) { |
| 80 | StringRef Str(s, LocalOffset - PrevOffset - 1); |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 81 | auto Pair = Strings.insert(std::make_pair(Str, StringOffset)); |
| 82 | if (Pair.second) { |
| 83 | Out.SwitchSection(StrSection); |
| 84 | Out.EmitBytes( |
| 85 | StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1)); |
| 86 | StringOffset += Str.size() + 1; |
| 87 | } |
| 88 | OffsetRemapping[PrevOffset] = Pair.first->second; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 89 | PrevOffset = LocalOffset; |
| 90 | } |
| 91 | |
| 92 | Data = DataExtractor(CurStrOffsetSection, true, 0); |
| 93 | |
| 94 | Out.SwitchSection(StrOffsetSection); |
| 95 | |
| 96 | uint32_t Offset = 0; |
| 97 | uint64_t Size = CurStrOffsetSection.size(); |
| 98 | while (Offset < Size) { |
| 99 | auto OldOffset = Data.getU32(&Offset); |
| 100 | auto NewOffset = OffsetRemapping[OldOffset]; |
| 101 | Out.EmitIntValue(NewOffset, 4); |
| 102 | } |
| 103 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 104 | return std::error_code(); |
| 105 | } |
| 106 | |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 107 | static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) { |
| 108 | uint64_t CurCode; |
| 109 | uint32_t Offset = 0; |
| 110 | DataExtractor AbbrevData(Abbrev, true, 0); |
| 111 | while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) { |
| 112 | // Tag |
| 113 | AbbrevData.getULEB128(&Offset); |
| 114 | // DW_CHILDREN |
| 115 | AbbrevData.getU8(&Offset); |
| 116 | // Attributes |
| 117 | while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset)) |
| 118 | ; |
| 119 | } |
| 120 | return Offset; |
| 121 | } |
| 122 | |
David Blaikie | f1958da | 2016-02-26 07:30:15 +0000 | [diff] [blame] | 123 | static const char *getCUName(StringRef Abbrev, StringRef Info, |
| 124 | StringRef StrOffsets, StringRef Str) { |
| 125 | uint32_t Offset = 0; |
| 126 | DataExtractor InfoData(Info, true, 0); |
| 127 | InfoData.getU32(&Offset); // Length |
| 128 | uint16_t Version = InfoData.getU16(&Offset); |
| 129 | InfoData.getU32(&Offset); // Abbrev offset (should be zero) |
| 130 | uint8_t AddrSize = InfoData.getU8(&Offset); |
| 131 | |
| 132 | uint32_t AbbrCode = InfoData.getULEB128(&Offset); |
| 133 | |
| 134 | DataExtractor AbbrevData(Abbrev, true, 0); |
| 135 | uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode); |
| 136 | uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset); |
| 137 | (void)Tag; |
| 138 | // FIXME: Real error handling |
| 139 | assert(Tag == dwarf::DW_TAG_compile_unit); |
| 140 | // DW_CHILDREN |
| 141 | AbbrevData.getU8(&AbbrevOffset); |
| 142 | uint32_t Name; |
| 143 | uint32_t Form; |
| 144 | while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) | |
| 145 | (Form = AbbrevData.getULEB128(&AbbrevOffset)) && |
| 146 | Name != dwarf::DW_AT_name) { |
| 147 | DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize); |
| 148 | } |
| 149 | // FIXME: Real error handling |
| 150 | assert(Name == dwarf::DW_AT_name); |
| 151 | auto StrIndex = InfoData.getULEB128(&Offset); |
| 152 | |
| 153 | DataExtractor StrOffsetsData(StrOffsets, true, 0); |
| 154 | uint32_t StrOffsetsOffset = 4 * StrIndex; |
| 155 | uint32_t StrOffset = StrOffsetsData.getU32(&StrOffsetsOffset); |
| 156 | DataExtractor StrData(Str, true, 0); |
| 157 | return StrData.getCStr(&StrOffset); |
| 158 | } |
| 159 | |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 160 | static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) { |
| 161 | uint32_t Offset = 0; |
| 162 | DataExtractor InfoData(Info, true, 0); |
| 163 | InfoData.getU32(&Offset); // Length |
| 164 | uint16_t Version = InfoData.getU16(&Offset); |
| 165 | InfoData.getU32(&Offset); // Abbrev offset (should be zero) |
| 166 | uint8_t AddrSize = InfoData.getU8(&Offset); |
| 167 | |
| 168 | uint32_t AbbrCode = InfoData.getULEB128(&Offset); |
| 169 | |
| 170 | DataExtractor AbbrevData(Abbrev, true, 0); |
| 171 | uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode); |
| 172 | uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset); |
| 173 | (void)Tag; |
| 174 | // FIXME: Real error handling |
| 175 | assert(Tag == dwarf::DW_TAG_compile_unit); |
| 176 | // DW_CHILDREN |
| 177 | AbbrevData.getU8(&AbbrevOffset); |
| 178 | uint32_t Name; |
| 179 | uint32_t Form; |
| 180 | while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) | |
| 181 | (Form = AbbrevData.getULEB128(&AbbrevOffset)) && |
| 182 | Name != dwarf::DW_AT_GNU_dwo_id) { |
| 183 | DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize); |
| 184 | } |
| 185 | // FIXME: Real error handling |
| 186 | assert(Name == dwarf::DW_AT_GNU_dwo_id); |
| 187 | return InfoData.getU64(&Offset); |
| 188 | } |
| 189 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 190 | struct UnitIndexEntry { |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 191 | DWARFUnitIndex::Entry::SectionContribution Contributions[8]; |
David Blaikie | f1958da | 2016-02-26 07:30:15 +0000 | [diff] [blame] | 192 | std::string Name; |
| 193 | StringRef DWPName; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 194 | }; |
| 195 | |
David Blaikie | f1958da | 2016-02-26 07:30:15 +0000 | [diff] [blame] | 196 | StringRef getSubsection(StringRef Section, const DWARFUnitIndex::Entry &Entry, DWARFSectionKind Kind) { |
| 197 | const auto *Off = Entry.getOffset(Kind); |
| 198 | if (!Off) |
| 199 | return StringRef(); |
| 200 | return Section.substr(Off->Offset, Off->Length); |
| 201 | } |
| 202 | |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 203 | static void addAllTypesFromDWP( |
| 204 | MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries, |
| 205 | const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types, |
| 206 | const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) { |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 207 | Out.SwitchSection(OutputTypes); |
| 208 | for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) { |
| 209 | auto *I = E.getOffsets(); |
| 210 | if (!I) |
| 211 | continue; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 212 | auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry)); |
| 213 | if (!P.second) |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 214 | continue; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 215 | auto &Entry = P.first->second; |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 216 | // Zero out the debug_info contribution |
| 217 | Entry.Contributions[0] = {}; |
| 218 | for (auto Kind : TUIndex.getColumnKinds()) { |
| 219 | auto &C = Entry.Contributions[Kind - DW_SECT_INFO]; |
| 220 | C.Offset += I->Offset; |
| 221 | C.Length = I->Length; |
| 222 | ++I; |
| 223 | } |
| 224 | auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; |
| 225 | Out.EmitBytes(Types.substr( |
| 226 | C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset, |
| 227 | C.Length)); |
| 228 | C.Offset = TypesOffset; |
| 229 | TypesOffset += C.Length; |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 233 | static void addAllTypes(MCStreamer &Out, |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 234 | MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries, |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 235 | MCSection *OutputTypes, StringRef Types, |
| 236 | const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) { |
| 237 | if (Types.empty()) |
| 238 | return; |
| 239 | |
| 240 | Out.SwitchSection(OutputTypes); |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 241 | uint32_t Offset = 0; |
| 242 | DataExtractor Data(Types, true, 0); |
| 243 | while (Data.isValidOffset(Offset)) { |
David Blaikie | f5cb627 | 2015-12-14 07:42:00 +0000 | [diff] [blame] | 244 | UnitIndexEntry Entry = CUEntry; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 245 | // Zero out the debug_info contribution |
| 246 | Entry.Contributions[0] = {}; |
| 247 | auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; |
David Blaikie | f5cb627 | 2015-12-14 07:42:00 +0000 | [diff] [blame] | 248 | C.Offset = TypesOffset; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 249 | auto PrevOffset = Offset; |
| 250 | // Length of the unit, including the 4 byte length field. |
| 251 | C.Length = Data.getU32(&Offset) + 4; |
| 252 | |
| 253 | Data.getU16(&Offset); // Version |
| 254 | Data.getU32(&Offset); // Abbrev offset |
| 255 | Data.getU8(&Offset); // Address size |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 256 | auto Signature = Data.getU64(&Offset); |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 257 | Offset = PrevOffset + C.Length; |
David Blaikie | f5cb627 | 2015-12-14 07:42:00 +0000 | [diff] [blame] | 258 | |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 259 | auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry)); |
| 260 | if (!P.second) |
David Blaikie | f5cb627 | 2015-12-14 07:42:00 +0000 | [diff] [blame] | 261 | continue; |
| 262 | |
| 263 | Out.EmitBytes(Types.substr(PrevOffset, C.Length)); |
| 264 | TypesOffset += C.Length; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | |
| 268 | static void |
| 269 | writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets, |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 270 | const MapVector<uint64_t, UnitIndexEntry> &IndexEntries, |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 271 | uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) { |
| 272 | for (const auto &E : IndexEntries) |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 273 | for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i) |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 274 | if (ContributionOffsets[i]) |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 275 | Out.EmitIntValue(E.second.Contributions[i].*Field, 4); |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 276 | } |
| 277 | |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 278 | static void |
| 279 | writeIndex(MCStreamer &Out, MCSection *Section, |
| 280 | ArrayRef<unsigned> ContributionOffsets, |
| 281 | const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) { |
David Blaikie | 5d6d4dc | 2016-02-26 07:04:58 +0000 | [diff] [blame] | 282 | if (IndexEntries.empty()) |
| 283 | return; |
| 284 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 285 | unsigned Columns = 0; |
| 286 | for (auto &C : ContributionOffsets) |
| 287 | if (C) |
| 288 | ++Columns; |
| 289 | |
| 290 | std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2)); |
| 291 | uint64_t Mask = Buckets.size() - 1; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 292 | size_t i = 0; |
| 293 | for (const auto &P : IndexEntries) { |
| 294 | auto S = P.first; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 295 | auto H = S & Mask; |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 296 | while (Buckets[H]) { |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 297 | assert(S != IndexEntries.begin()[Buckets[H] - 1].first && |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 298 | "Duplicate unit"); |
Benjamin Kramer | e593094 | 2016-02-18 13:23:17 +0000 | [diff] [blame] | 299 | H = (H + (((S >> 32) & Mask) | 1)) % Buckets.size(); |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 300 | } |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 301 | Buckets[H] = i + 1; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 302 | ++i; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | Out.SwitchSection(Section); |
| 306 | Out.EmitIntValue(2, 4); // Version |
| 307 | Out.EmitIntValue(Columns, 4); // Columns |
| 308 | Out.EmitIntValue(IndexEntries.size(), 4); // Num Units |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 309 | Out.EmitIntValue(Buckets.size(), 4); // Num Buckets |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 310 | |
| 311 | // Write the signatures. |
| 312 | for (const auto &I : Buckets) |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 313 | Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8); |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 314 | |
| 315 | // Write the indexes. |
| 316 | for (const auto &I : Buckets) |
| 317 | Out.EmitIntValue(I, 4); |
| 318 | |
| 319 | // Write the column headers (which sections will appear in the table) |
| 320 | for (size_t i = 0; i != ContributionOffsets.size(); ++i) |
| 321 | if (ContributionOffsets[i]) |
| 322 | Out.EmitIntValue(i + DW_SECT_INFO, 4); |
| 323 | |
| 324 | // Write the offsets. |
| 325 | writeIndexTable(Out, ContributionOffsets, IndexEntries, |
| 326 | &DWARFUnitIndex::Entry::SectionContribution::Offset); |
| 327 | |
| 328 | // Write the lengths. |
| 329 | writeIndexTable(Out, ContributionOffsets, IndexEntries, |
| 330 | &DWARFUnitIndex::Entry::SectionContribution::Length); |
| 331 | } |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 332 | static bool consumeCompressedDebugSectionHeader(StringRef &data, |
| 333 | uint64_t &OriginalSize) { |
| 334 | // Consume "ZLIB" prefix. |
| 335 | if (!data.startswith("ZLIB")) |
| 336 | return false; |
| 337 | data = data.substr(4); |
| 338 | // Consume uncompressed section size (big-endian 8 bytes). |
| 339 | DataExtractor extractor(data, false, 8); |
| 340 | uint32_t Offset = 0; |
| 341 | OriginalSize = extractor.getU64(&Offset); |
| 342 | if (Offset == 0) |
| 343 | return false; |
| 344 | data = data.substr(Offset); |
| 345 | return true; |
| 346 | } |
| 347 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 348 | static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) { |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 349 | const auto &MCOFI = *Out.getContext().getObjectFileInfo(); |
| 350 | MCSection *const StrSection = MCOFI.getDwarfStrDWOSection(); |
| 351 | MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection(); |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 352 | MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection(); |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 353 | MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection(); |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 354 | MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection(); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 355 | const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = { |
| 356 | {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}}, |
| 357 | {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}}, |
| 358 | {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}}, |
| 359 | {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}}, |
| 360 | {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}}, |
David Blaikie | b702025 | 2015-12-04 21:16:42 +0000 | [diff] [blame] | 361 | {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}}, |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 362 | {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}, |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 363 | {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}}, |
| 364 | {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}}; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 365 | |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 366 | MapVector<uint64_t, UnitIndexEntry> IndexEntries; |
| 367 | MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 368 | |
| 369 | StringMap<uint32_t> Strings; |
| 370 | uint32_t StringOffset = 0; |
| 371 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 372 | uint32_t ContributionOffsets[8] = {}; |
| 373 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 374 | for (const auto &Input : Inputs) { |
| 375 | auto ErrOrObj = object::ObjectFile::createObjectFile(Input); |
| 376 | if (!ErrOrObj) |
| 377 | return ErrOrObj.getError(); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 378 | |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 379 | UnitIndexEntry CurEntry = {}; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 380 | |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 381 | StringRef CurStrSection; |
| 382 | StringRef CurStrOffsetSection; |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 383 | StringRef CurTypesSection; |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 384 | StringRef InfoSection; |
| 385 | StringRef AbbrevSection; |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 386 | StringRef CurCUIndexSection; |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 387 | StringRef CurTUIndexSection; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 388 | |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 389 | SmallVector<SmallString<32>, 4> UncompressedSections; |
| 390 | |
David Blaikie | ddb2736 | 2016-03-01 21:24:04 +0000 | [diff] [blame] | 391 | for (const auto &Section : ErrOrObj->getBinary()->sections()) { |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 392 | if (Section.isBSS()) |
| 393 | continue; |
| 394 | if (Section.isVirtual()) |
| 395 | continue; |
| 396 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 397 | StringRef Name; |
| 398 | if (std::error_code Err = Section.getName(Name)) |
| 399 | return Err; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 400 | |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 401 | Name = Name.substr(Name.find_first_not_of("._")); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 402 | |
| 403 | StringRef Contents; |
| 404 | if (auto Err = Section.getContents(Contents)) |
| 405 | return Err; |
| 406 | |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 407 | if (Name.startswith("zdebug_")) { |
| 408 | uint64_t OriginalSize; |
| 409 | if (!zlib::isAvailable() || |
| 410 | !consumeCompressedDebugSectionHeader(Contents, OriginalSize)) |
David Blaikie | 9a5d364 | 2016-02-19 02:03:45 +0000 | [diff] [blame] | 411 | return make_error_code(std::errc::invalid_argument); |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 412 | UncompressedSections.resize(UncompressedSections.size() + 1); |
| 413 | if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) != |
| 414 | zlib::StatusOK) { |
| 415 | UncompressedSections.pop_back(); |
| 416 | continue; |
| 417 | } |
| 418 | Name = Name.substr(1); |
| 419 | Contents = UncompressedSections.back(); |
| 420 | } |
| 421 | |
| 422 | auto SectionPair = KnownSections.find(Name); |
| 423 | if (SectionPair == KnownSections.end()) |
| 424 | continue; |
| 425 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 426 | if (DWARFSectionKind Kind = SectionPair->second.second) { |
| 427 | auto Index = Kind - DW_SECT_INFO; |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 428 | if (Kind != DW_SECT_TYPES) { |
| 429 | CurEntry.Contributions[Index].Offset = ContributionOffsets[Index]; |
| 430 | ContributionOffsets[Index] += |
| 431 | (CurEntry.Contributions[Index].Length = Contents.size()); |
| 432 | } |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 433 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 434 | switch (Kind) { |
| 435 | case DW_SECT_INFO: |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 436 | InfoSection = Contents; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 437 | break; |
| 438 | case DW_SECT_ABBREV: |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 439 | AbbrevSection = Contents; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 440 | break; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 441 | default: |
| 442 | break; |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 443 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | MCSection *OutSection = SectionPair->second.first; |
| 447 | if (OutSection == StrOffsetSection) |
| 448 | CurStrOffsetSection = Contents; |
| 449 | else if (OutSection == StrSection) |
| 450 | CurStrSection = Contents; |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 451 | else if (OutSection == TypesSection) |
| 452 | CurTypesSection = Contents; |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 453 | else if (OutSection == CUIndexSection) |
| 454 | CurCUIndexSection = Contents; |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 455 | else if (OutSection == TUIndexSection) |
| 456 | CurTUIndexSection = Contents; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 457 | else { |
| 458 | Out.SwitchSection(OutSection); |
| 459 | Out.EmitBytes(Contents); |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 460 | } |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 461 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 462 | |
David Blaikie | 5d6d4dc | 2016-02-26 07:04:58 +0000 | [diff] [blame] | 463 | if (InfoSection.empty()) |
| 464 | continue; |
| 465 | |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 466 | if (!CurCUIndexSection.empty()) { |
| 467 | DWARFUnitIndex CUIndex(DW_SECT_INFO); |
David Blaikie | ddb2736 | 2016-03-01 21:24:04 +0000 | [diff] [blame] | 468 | DataExtractor CUIndexData(CurCUIndexSection, |
| 469 | ErrOrObj->getBinary()->isLittleEndian(), 0); |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 470 | if (!CUIndex.parse(CUIndexData)) |
| 471 | return make_error_code(std::errc::invalid_argument); |
| 472 | |
| 473 | for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) { |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 474 | auto *I = E.getOffsets(); |
| 475 | if (!I) |
| 476 | continue; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 477 | auto P = |
| 478 | IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry)); |
David Blaikie | f1958da | 2016-02-26 07:30:15 +0000 | [diff] [blame] | 479 | const char* Name = getCUName( |
| 480 | getSubsection(AbbrevSection, E, DW_SECT_ABBREV), |
| 481 | getSubsection(InfoSection, E, DW_SECT_INFO), |
| 482 | getSubsection(CurStrOffsetSection, E, DW_SECT_STR_OFFSETS), |
| 483 | CurStrSection); |
| 484 | if (!P.second) { |
| 485 | auto &PrevE = *P.first; |
| 486 | std::cerr << "Duplicate DWO ID (" << PrevE.first << ") in '" << PrevE.second.Name << "' "; |
| 487 | if (!PrevE.second.DWPName.empty()) |
| 488 | std::cerr << "(from '" << PrevE.second.DWPName.str() << "') "; |
| 489 | std::cerr << "and '" << Name << "' (from '" << Input << "')\n"; |
| 490 | return make_error_code(std::errc::invalid_argument); |
| 491 | } |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 492 | auto &NewEntry = P.first->second; |
David Blaikie | f1958da | 2016-02-26 07:30:15 +0000 | [diff] [blame] | 493 | NewEntry.Name = Name; |
| 494 | NewEntry.DWPName = Input; |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 495 | for (auto Kind : CUIndex.getColumnKinds()) { |
| 496 | auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO]; |
| 497 | C.Offset += I->Offset; |
| 498 | C.Length = I->Length; |
| 499 | ++I; |
| 500 | } |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 501 | } |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 502 | |
| 503 | if (!CurTypesSection.empty()) { |
| 504 | if (CurTUIndexSection.empty()) |
| 505 | return make_error_code(std::errc::invalid_argument); |
| 506 | DWARFUnitIndex TUIndex(DW_SECT_TYPES); |
David Blaikie | ddb2736 | 2016-03-01 21:24:04 +0000 | [diff] [blame] | 507 | DataExtractor TUIndexData(CurTUIndexSection, |
| 508 | ErrOrObj->getBinary()->isLittleEndian(), 0); |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 509 | if (!TUIndex.parse(TUIndexData)) |
| 510 | return make_error_code(std::errc::invalid_argument); |
| 511 | addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection, |
| 512 | CurTypesSection, CurEntry, |
| 513 | ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); |
| 514 | } |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 515 | } else { |
David Blaikie | f1958da | 2016-02-26 07:30:15 +0000 | [diff] [blame] | 516 | auto P = IndexEntries.insert( |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame] | 517 | std::make_pair(getCUSignature(AbbrevSection, InfoSection), CurEntry)); |
David Blaikie | f1958da | 2016-02-26 07:30:15 +0000 | [diff] [blame] | 518 | const char *Name = getCUName(AbbrevSection, InfoSection, |
| 519 | CurStrOffsetSection, CurStrSection); |
| 520 | if (!P.second) { |
| 521 | auto &E = *P.first; |
| 522 | std::cerr << "Duplicate DWO ID (" << E.first << ") in '" << Name |
| 523 | << "' "; |
| 524 | if (!E.second.DWPName.empty()) |
| 525 | std::cerr << "(from '" << E.second.DWPName.str() << "') "; |
| 526 | std::cerr << "and '" << E.second.Name << "'\n"; |
| 527 | return make_error_code(std::errc::invalid_argument); |
| 528 | } |
| 529 | P.first->second.Name = Name; |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 530 | addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, |
| 531 | CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 532 | } |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 533 | |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 534 | if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset, |
| 535 | StrSection, StrOffsetSection, |
| 536 | CurStrSection, CurStrOffsetSection)) |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 537 | return Err; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 538 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 539 | |
David Blaikie | 5d6d4dc | 2016-02-26 07:04:58 +0000 | [diff] [blame] | 540 | // Lie about there being no info contributions so the TU index only includes |
| 541 | // the type unit contribution |
| 542 | ContributionOffsets[0] = 0; |
| 543 | writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets, |
| 544 | TypeIndexEntries); |
David Blaikie | b3757c0 | 2015-12-02 22:01:56 +0000 | [diff] [blame] | 545 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 546 | // Lie about the type contribution |
| 547 | ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0; |
| 548 | // Unlie about the info contribution |
| 549 | ContributionOffsets[0] = 1; |
David Blaikie | 7c4ffe0 | 2015-12-04 21:30:23 +0000 | [diff] [blame] | 550 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 551 | writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets, |
| 552 | IndexEntries); |
David Blaikie | ddb2736 | 2016-03-01 21:24:04 +0000 | [diff] [blame] | 553 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 554 | return std::error_code(); |
| 555 | } |
| 556 | |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 557 | int main(int argc, char **argv) { |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 558 | |
| 559 | ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files"); |
| 560 | |
| 561 | llvm::InitializeAllTargetInfos(); |
| 562 | llvm::InitializeAllTargetMCs(); |
| 563 | llvm::InitializeAllTargets(); |
| 564 | llvm::InitializeAllAsmPrinters(); |
| 565 | |
| 566 | std::string ErrorStr; |
| 567 | StringRef Context = "dwarf streamer init"; |
| 568 | |
| 569 | Triple TheTriple("x86_64-linux-gnu"); |
| 570 | |
| 571 | // Get the target. |
| 572 | const Target *TheTarget = |
| 573 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
| 574 | if (!TheTarget) |
| 575 | return error(ErrorStr, Context); |
| 576 | std::string TripleName = TheTriple.getTriple(); |
| 577 | |
| 578 | // Create all the MC Objects. |
| 579 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 580 | if (!MRI) |
| 581 | return error(Twine("no register info for target ") + TripleName, Context); |
| 582 | |
| 583 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 584 | if (!MAI) |
| 585 | return error("no asm info for target " + TripleName, Context); |
| 586 | |
| 587 | MCObjectFileInfo MOFI; |
| 588 | MCContext MC(MAI.get(), MRI.get(), &MOFI); |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 589 | MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC); |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 590 | |
| 591 | auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); |
| 592 | if (!MAB) |
| 593 | return error("no asm backend for target " + TripleName, Context); |
| 594 | |
| 595 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 596 | if (!MII) |
| 597 | return error("no instr info info for target " + TripleName, Context); |
| 598 | |
| 599 | std::unique_ptr<MCSubtargetInfo> MSTI( |
| 600 | TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 601 | if (!MSTI) |
| 602 | return error("no subtarget info for target " + TripleName, Context); |
| 603 | |
| 604 | MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC); |
| 605 | if (!MCE) |
| 606 | return error("no code emitter for target " + TripleName, Context); |
| 607 | |
| 608 | // Create the output file. |
| 609 | std::error_code EC; |
| 610 | raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None); |
| 611 | if (EC) |
| 612 | return error(Twine(OutputFilename) + ": " + EC.message(), Context); |
| 613 | |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 614 | MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 615 | std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 616 | TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll, |
| 617 | MCOptions.MCIncrementalLinkerCompatible, |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 618 | /*DWARFMustBeAtTheEnd*/ false)); |
| 619 | if (!MS) |
| 620 | return error("no object streamer for target " + TripleName, Context); |
| 621 | |
| 622 | if (auto Err = write(*MS, InputFiles)) |
| 623 | return error(Err.message(), "Writing DWP file"); |
David Blaikie | ddb2736 | 2016-03-01 21:24:04 +0000 | [diff] [blame] | 624 | |
| 625 | MS->Finish(); |
David Blaikie | df05525 | 2015-12-01 00:48:34 +0000 | [diff] [blame] | 626 | } |