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