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