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