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