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