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