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