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