David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 1 | #include "llvm/ADT/MapVector.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 2 | #include "llvm/ADT/STLExtras.h" |
| 3 | #include "llvm/ADT/StringSet.h" |
| 4 | #include "llvm/CodeGen/AsmPrinter.h" |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 5 | #include "llvm/DebugInfo/DWARF/DWARFFormValue.h" |
| 6 | #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 7 | #include "llvm/MC/MCAsmInfo.h" |
| 8 | #include "llvm/MC/MCContext.h" |
| 9 | #include "llvm/MC/MCInstrInfo.h" |
| 10 | #include "llvm/MC/MCObjectFileInfo.h" |
| 11 | #include "llvm/MC/MCRegisterInfo.h" |
| 12 | #include "llvm/MC/MCSectionELF.h" |
| 13 | #include "llvm/MC/MCStreamer.h" |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 14 | #include "llvm/MC/MCTargetOptionsCommandFlags.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 15 | #include "llvm/Object/ObjectFile.h" |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Compression.h" |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 17 | #include "llvm/Support/DataExtractor.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 18 | #include "llvm/Support/FileSystem.h" |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 19 | #include "llvm/Support/MathExtras.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/MemoryBuffer.h" |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Options.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 22 | #include "llvm/Support/TargetRegistry.h" |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 23 | #include "llvm/Support/TargetSelect.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 24 | #include "llvm/Support/raw_ostream.h" |
| 25 | #include "llvm/Target/TargetMachine.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 26 | #include <list> |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 27 | #include <memory> |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 28 | #include <system_error> |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 29 | #include <unordered_set> |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 30 | |
| 31 | using namespace llvm; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 32 | using namespace llvm::object; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 33 | using namespace cl; |
| 34 | |
| 35 | OptionCategory DwpCategory("Specific Options"); |
| 36 | static list<std::string> InputFiles(Positional, OneOrMore, |
| 37 | desc("<input files>"), cat(DwpCategory)); |
| 38 | |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 39 | static opt<std::string> OutputFilename(Required, "o", |
| 40 | desc("Specify the output file."), |
| 41 | value_desc("filename"), |
| 42 | cat(DwpCategory)); |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 43 | |
| 44 | static int error(const Twine &Error, const Twine &Context) { |
| 45 | errs() << Twine("while processing ") + Context + ":\n"; |
| 46 | errs() << Twine("error: ") + Error + "\n"; |
| 47 | return 1; |
| 48 | } |
| 49 | |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 50 | static std::error_code |
| 51 | writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings, |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 52 | uint32_t &StringOffset, MCSection *StrSection, |
| 53 | MCSection *StrOffsetSection, StringRef CurStrSection, |
| 54 | StringRef CurStrOffsetSection) { |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 55 | // Could possibly produce an error or warning if one of these was non-null but |
| 56 | // the other was null. |
| 57 | if (CurStrSection.empty() || CurStrOffsetSection.empty()) |
| 58 | return std::error_code(); |
| 59 | |
| 60 | DenseMap<uint32_t, uint32_t> OffsetRemapping; |
| 61 | |
| 62 | DataExtractor Data(CurStrSection, true, 0); |
| 63 | uint32_t LocalOffset = 0; |
| 64 | uint32_t PrevOffset = 0; |
| 65 | while (const char *s = Data.getCStr(&LocalOffset)) { |
| 66 | StringRef Str(s, LocalOffset - PrevOffset - 1); |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 67 | auto Pair = Strings.insert(std::make_pair(Str, StringOffset)); |
| 68 | if (Pair.second) { |
| 69 | Out.SwitchSection(StrSection); |
| 70 | Out.EmitBytes( |
| 71 | StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1)); |
| 72 | StringOffset += Str.size() + 1; |
| 73 | } |
| 74 | OffsetRemapping[PrevOffset] = Pair.first->second; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 75 | PrevOffset = LocalOffset; |
| 76 | } |
| 77 | |
| 78 | Data = DataExtractor(CurStrOffsetSection, true, 0); |
| 79 | |
| 80 | Out.SwitchSection(StrOffsetSection); |
| 81 | |
| 82 | uint32_t Offset = 0; |
| 83 | uint64_t Size = CurStrOffsetSection.size(); |
| 84 | while (Offset < Size) { |
| 85 | auto OldOffset = Data.getU32(&Offset); |
| 86 | auto NewOffset = OffsetRemapping[OldOffset]; |
| 87 | Out.EmitIntValue(NewOffset, 4); |
| 88 | } |
| 89 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 90 | return std::error_code(); |
| 91 | } |
| 92 | |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 93 | static uint32_t getCUAbbrev(StringRef Abbrev, uint64_t AbbrCode) { |
| 94 | uint64_t CurCode; |
| 95 | uint32_t Offset = 0; |
| 96 | DataExtractor AbbrevData(Abbrev, true, 0); |
| 97 | while ((CurCode = AbbrevData.getULEB128(&Offset)) != AbbrCode) { |
| 98 | // Tag |
| 99 | AbbrevData.getULEB128(&Offset); |
| 100 | // DW_CHILDREN |
| 101 | AbbrevData.getU8(&Offset); |
| 102 | // Attributes |
| 103 | while (AbbrevData.getULEB128(&Offset) | AbbrevData.getULEB128(&Offset)) |
| 104 | ; |
| 105 | } |
| 106 | return Offset; |
| 107 | } |
| 108 | |
| 109 | static uint64_t getCUSignature(StringRef Abbrev, StringRef Info) { |
| 110 | uint32_t Offset = 0; |
| 111 | DataExtractor InfoData(Info, true, 0); |
| 112 | InfoData.getU32(&Offset); // Length |
| 113 | uint16_t Version = InfoData.getU16(&Offset); |
| 114 | InfoData.getU32(&Offset); // Abbrev offset (should be zero) |
| 115 | uint8_t AddrSize = InfoData.getU8(&Offset); |
| 116 | |
| 117 | uint32_t AbbrCode = InfoData.getULEB128(&Offset); |
| 118 | |
| 119 | DataExtractor AbbrevData(Abbrev, true, 0); |
| 120 | uint32_t AbbrevOffset = getCUAbbrev(Abbrev, AbbrCode); |
| 121 | uint64_t Tag = AbbrevData.getULEB128(&AbbrevOffset); |
| 122 | (void)Tag; |
| 123 | // FIXME: Real error handling |
| 124 | assert(Tag == dwarf::DW_TAG_compile_unit); |
| 125 | // DW_CHILDREN |
| 126 | AbbrevData.getU8(&AbbrevOffset); |
| 127 | uint32_t Name; |
| 128 | uint32_t Form; |
| 129 | while ((Name = AbbrevData.getULEB128(&AbbrevOffset)) | |
| 130 | (Form = AbbrevData.getULEB128(&AbbrevOffset)) && |
| 131 | Name != dwarf::DW_AT_GNU_dwo_id) { |
| 132 | DWARFFormValue::skipValue(Form, InfoData, &Offset, Version, AddrSize); |
| 133 | } |
| 134 | // FIXME: Real error handling |
| 135 | assert(Name == dwarf::DW_AT_GNU_dwo_id); |
| 136 | return InfoData.getU64(&Offset); |
| 137 | } |
| 138 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 139 | struct UnitIndexEntry { |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 140 | DWARFUnitIndex::Entry::SectionContribution Contributions[8]; |
| 141 | }; |
| 142 | |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 143 | static void addAllTypesFromDWP( |
| 144 | MCStreamer &Out, MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries, |
| 145 | const DWARFUnitIndex &TUIndex, MCSection *OutputTypes, StringRef Types, |
| 146 | const UnitIndexEntry &TUEntry, uint32_t &TypesOffset) { |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 147 | Out.SwitchSection(OutputTypes); |
| 148 | for (const DWARFUnitIndex::Entry &E : TUIndex.getRows()) { |
| 149 | auto *I = E.getOffsets(); |
| 150 | if (!I) |
| 151 | continue; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 152 | auto P = TypeIndexEntries.insert(std::make_pair(E.getSignature(), TUEntry)); |
| 153 | if (!P.second) |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 154 | continue; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 155 | auto &Entry = P.first->second; |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 156 | // Zero out the debug_info contribution |
| 157 | Entry.Contributions[0] = {}; |
| 158 | for (auto Kind : TUIndex.getColumnKinds()) { |
| 159 | auto &C = Entry.Contributions[Kind - DW_SECT_INFO]; |
| 160 | C.Offset += I->Offset; |
| 161 | C.Length = I->Length; |
| 162 | ++I; |
| 163 | } |
| 164 | auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; |
| 165 | Out.EmitBytes(Types.substr( |
| 166 | C.Offset - TUEntry.Contributions[DW_SECT_TYPES - DW_SECT_INFO].Offset, |
| 167 | C.Length)); |
| 168 | C.Offset = TypesOffset; |
| 169 | TypesOffset += C.Length; |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 170 | } |
| 171 | } |
| 172 | |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 173 | static void addAllTypes(MCStreamer &Out, |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 174 | MapVector<uint64_t, UnitIndexEntry> &TypeIndexEntries, |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 175 | MCSection *OutputTypes, StringRef Types, |
| 176 | const UnitIndexEntry &CUEntry, uint32_t &TypesOffset) { |
| 177 | if (Types.empty()) |
| 178 | return; |
| 179 | |
| 180 | Out.SwitchSection(OutputTypes); |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 181 | uint32_t Offset = 0; |
| 182 | DataExtractor Data(Types, true, 0); |
| 183 | while (Data.isValidOffset(Offset)) { |
David Blaikie | f5cb627 | 2015-12-14 07:42:00 +0000 | [diff] [blame] | 184 | UnitIndexEntry Entry = CUEntry; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 185 | // Zero out the debug_info contribution |
| 186 | Entry.Contributions[0] = {}; |
| 187 | auto &C = Entry.Contributions[DW_SECT_TYPES - DW_SECT_INFO]; |
David Blaikie | f5cb627 | 2015-12-14 07:42:00 +0000 | [diff] [blame] | 188 | C.Offset = TypesOffset; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 189 | auto PrevOffset = Offset; |
| 190 | // Length of the unit, including the 4 byte length field. |
| 191 | C.Length = Data.getU32(&Offset) + 4; |
| 192 | |
| 193 | Data.getU16(&Offset); // Version |
| 194 | Data.getU32(&Offset); // Abbrev offset |
| 195 | Data.getU8(&Offset); // Address size |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 196 | auto Signature = Data.getU64(&Offset); |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 197 | Offset = PrevOffset + C.Length; |
David Blaikie | f5cb627 | 2015-12-14 07:42:00 +0000 | [diff] [blame] | 198 | |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 199 | auto P = TypeIndexEntries.insert(std::make_pair(Signature, Entry)); |
| 200 | if (!P.second) |
David Blaikie | f5cb627 | 2015-12-14 07:42:00 +0000 | [diff] [blame] | 201 | continue; |
| 202 | |
| 203 | Out.EmitBytes(Types.substr(PrevOffset, C.Length)); |
| 204 | TypesOffset += C.Length; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 205 | } |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | writeIndexTable(MCStreamer &Out, ArrayRef<unsigned> ContributionOffsets, |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 210 | const MapVector<uint64_t, UnitIndexEntry> &IndexEntries, |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 211 | uint32_t DWARFUnitIndex::Entry::SectionContribution::*Field) { |
| 212 | for (const auto &E : IndexEntries) |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 213 | for (size_t i = 0; i != array_lengthof(E.second.Contributions); ++i) |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 214 | if (ContributionOffsets[i]) |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 215 | Out.EmitIntValue(E.second.Contributions[i].*Field, 4); |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 216 | } |
| 217 | |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 218 | static void |
| 219 | writeIndex(MCStreamer &Out, MCSection *Section, |
| 220 | ArrayRef<unsigned> ContributionOffsets, |
| 221 | const MapVector<uint64_t, UnitIndexEntry> &IndexEntries) { |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 222 | unsigned Columns = 0; |
| 223 | for (auto &C : ContributionOffsets) |
| 224 | if (C) |
| 225 | ++Columns; |
| 226 | |
| 227 | std::vector<unsigned> Buckets(NextPowerOf2(3 * IndexEntries.size() / 2)); |
| 228 | uint64_t Mask = Buckets.size() - 1; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 229 | size_t i = 0; |
| 230 | for (const auto &P : IndexEntries) { |
| 231 | auto S = P.first; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 232 | auto H = S & Mask; |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 233 | while (Buckets[H]) { |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 234 | assert(S != IndexEntries.begin()[Buckets[H] - 1].first && |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 235 | "Duplicate unit"); |
Benjamin Kramer | e593094 | 2016-02-18 13:23:17 +0000 | [diff] [blame] | 236 | H = (H + (((S >> 32) & Mask) | 1)) % Buckets.size(); |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 237 | } |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 238 | Buckets[H] = i + 1; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 239 | ++i; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | Out.SwitchSection(Section); |
| 243 | Out.EmitIntValue(2, 4); // Version |
| 244 | Out.EmitIntValue(Columns, 4); // Columns |
| 245 | Out.EmitIntValue(IndexEntries.size(), 4); // Num Units |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 246 | Out.EmitIntValue(Buckets.size(), 4); // Num Buckets |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 247 | |
| 248 | // Write the signatures. |
| 249 | for (const auto &I : Buckets) |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 250 | Out.EmitIntValue(I ? IndexEntries.begin()[I - 1].first : 0, 8); |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 251 | |
| 252 | // Write the indexes. |
| 253 | for (const auto &I : Buckets) |
| 254 | Out.EmitIntValue(I, 4); |
| 255 | |
| 256 | // Write the column headers (which sections will appear in the table) |
| 257 | for (size_t i = 0; i != ContributionOffsets.size(); ++i) |
| 258 | if (ContributionOffsets[i]) |
| 259 | Out.EmitIntValue(i + DW_SECT_INFO, 4); |
| 260 | |
| 261 | // Write the offsets. |
| 262 | writeIndexTable(Out, ContributionOffsets, IndexEntries, |
| 263 | &DWARFUnitIndex::Entry::SectionContribution::Offset); |
| 264 | |
| 265 | // Write the lengths. |
| 266 | writeIndexTable(Out, ContributionOffsets, IndexEntries, |
| 267 | &DWARFUnitIndex::Entry::SectionContribution::Length); |
| 268 | } |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 269 | static bool consumeCompressedDebugSectionHeader(StringRef &data, |
| 270 | uint64_t &OriginalSize) { |
| 271 | // Consume "ZLIB" prefix. |
| 272 | if (!data.startswith("ZLIB")) |
| 273 | return false; |
| 274 | data = data.substr(4); |
| 275 | // Consume uncompressed section size (big-endian 8 bytes). |
| 276 | DataExtractor extractor(data, false, 8); |
| 277 | uint32_t Offset = 0; |
| 278 | OriginalSize = extractor.getU64(&Offset); |
| 279 | if (Offset == 0) |
| 280 | return false; |
| 281 | data = data.substr(Offset); |
| 282 | return true; |
| 283 | } |
| 284 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 285 | static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) { |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 286 | const auto &MCOFI = *Out.getContext().getObjectFileInfo(); |
| 287 | MCSection *const StrSection = MCOFI.getDwarfStrDWOSection(); |
| 288 | MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection(); |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 289 | MCSection *const TypesSection = MCOFI.getDwarfTypesDWOSection(); |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 290 | MCSection *const CUIndexSection = MCOFI.getDwarfCUIndexSection(); |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 291 | MCSection *const TUIndexSection = MCOFI.getDwarfTUIndexSection(); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 292 | const StringMap<std::pair<MCSection *, DWARFSectionKind>> KnownSections = { |
| 293 | {"debug_info.dwo", {MCOFI.getDwarfInfoDWOSection(), DW_SECT_INFO}}, |
| 294 | {"debug_types.dwo", {MCOFI.getDwarfTypesDWOSection(), DW_SECT_TYPES}}, |
| 295 | {"debug_str_offsets.dwo", {StrOffsetSection, DW_SECT_STR_OFFSETS}}, |
| 296 | {"debug_str.dwo", {StrSection, static_cast<DWARFSectionKind>(0)}}, |
| 297 | {"debug_loc.dwo", {MCOFI.getDwarfLocDWOSection(), DW_SECT_LOC}}, |
David Blaikie | b702025 | 2015-12-04 21:16:42 +0000 | [diff] [blame] | 298 | {"debug_line.dwo", {MCOFI.getDwarfLineDWOSection(), DW_SECT_LINE}}, |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 299 | {"debug_abbrev.dwo", {MCOFI.getDwarfAbbrevDWOSection(), DW_SECT_ABBREV}}, |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 300 | {"debug_cu_index", {CUIndexSection, static_cast<DWARFSectionKind>(0)}}, |
| 301 | {"debug_tu_index", {TUIndexSection, static_cast<DWARFSectionKind>(0)}}}; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 302 | |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 303 | MapVector<uint64_t, UnitIndexEntry> IndexEntries; |
| 304 | MapVector<uint64_t, UnitIndexEntry> TypeIndexEntries; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 305 | |
| 306 | StringMap<uint32_t> Strings; |
| 307 | uint32_t StringOffset = 0; |
| 308 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 309 | uint32_t ContributionOffsets[8] = {}; |
| 310 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 311 | for (const auto &Input : Inputs) { |
| 312 | auto ErrOrObj = object::ObjectFile::createObjectFile(Input); |
| 313 | if (!ErrOrObj) |
| 314 | return ErrOrObj.getError(); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 315 | |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 316 | UnitIndexEntry CurEntry = {}; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 317 | |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 318 | StringRef CurStrSection; |
| 319 | StringRef CurStrOffsetSection; |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 320 | StringRef CurTypesSection; |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 321 | StringRef InfoSection; |
| 322 | StringRef AbbrevSection; |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 323 | StringRef CurCUIndexSection; |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 324 | StringRef CurTUIndexSection; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 325 | |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 326 | SmallVector<SmallString<32>, 4> UncompressedSections; |
| 327 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 328 | for (const auto &Section : ErrOrObj->getBinary()->sections()) { |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 329 | if (Section.isBSS()) |
| 330 | continue; |
| 331 | if (Section.isVirtual()) |
| 332 | continue; |
| 333 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 334 | StringRef Name; |
| 335 | if (std::error_code Err = Section.getName(Name)) |
| 336 | return Err; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 337 | |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 338 | Name = Name.substr(Name.find_first_not_of("._")); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 339 | |
| 340 | StringRef Contents; |
| 341 | if (auto Err = Section.getContents(Contents)) |
| 342 | return Err; |
| 343 | |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 344 | if (Name.startswith("zdebug_")) { |
| 345 | uint64_t OriginalSize; |
| 346 | if (!zlib::isAvailable() || |
| 347 | !consumeCompressedDebugSectionHeader(Contents, OriginalSize)) |
David Blaikie | 9a5d364 | 2016-02-19 02:03:45 +0000 | [diff] [blame] | 348 | return make_error_code(std::errc::invalid_argument); |
David Blaikie | 74f5b28 | 2016-02-19 01:51:44 +0000 | [diff] [blame] | 349 | UncompressedSections.resize(UncompressedSections.size() + 1); |
| 350 | if (zlib::uncompress(Contents, UncompressedSections.back(), OriginalSize) != |
| 351 | zlib::StatusOK) { |
| 352 | UncompressedSections.pop_back(); |
| 353 | continue; |
| 354 | } |
| 355 | Name = Name.substr(1); |
| 356 | Contents = UncompressedSections.back(); |
| 357 | } |
| 358 | |
| 359 | auto SectionPair = KnownSections.find(Name); |
| 360 | if (SectionPair == KnownSections.end()) |
| 361 | continue; |
| 362 | |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 363 | if (DWARFSectionKind Kind = SectionPair->second.second) { |
| 364 | auto Index = Kind - DW_SECT_INFO; |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 365 | if (Kind != DW_SECT_TYPES) { |
| 366 | CurEntry.Contributions[Index].Offset = ContributionOffsets[Index]; |
| 367 | ContributionOffsets[Index] += |
| 368 | (CurEntry.Contributions[Index].Length = Contents.size()); |
| 369 | } |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 370 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 371 | switch (Kind) { |
| 372 | case DW_SECT_INFO: |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 373 | InfoSection = Contents; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 374 | break; |
| 375 | case DW_SECT_ABBREV: |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 376 | AbbrevSection = Contents; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 377 | break; |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 378 | default: |
| 379 | break; |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 380 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | MCSection *OutSection = SectionPair->second.first; |
| 384 | if (OutSection == StrOffsetSection) |
| 385 | CurStrOffsetSection = Contents; |
| 386 | else if (OutSection == StrSection) |
| 387 | CurStrSection = Contents; |
David Blaikie | c3826da | 2015-12-09 21:02:33 +0000 | [diff] [blame] | 388 | else if (OutSection == TypesSection) |
| 389 | CurTypesSection = Contents; |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 390 | else if (OutSection == CUIndexSection) |
| 391 | CurCUIndexSection = Contents; |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 392 | else if (OutSection == TUIndexSection) |
| 393 | CurTUIndexSection = Contents; |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 394 | else { |
| 395 | Out.SwitchSection(OutSection); |
| 396 | Out.EmitBytes(Contents); |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 397 | } |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 398 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 399 | |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 400 | assert(!AbbrevSection.empty()); |
| 401 | assert(!InfoSection.empty()); |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 402 | if (!CurCUIndexSection.empty()) { |
| 403 | DWARFUnitIndex CUIndex(DW_SECT_INFO); |
| 404 | DataExtractor CUIndexData(CurCUIndexSection, |
| 405 | ErrOrObj->getBinary()->isLittleEndian(), 0); |
| 406 | if (!CUIndex.parse(CUIndexData)) |
| 407 | return make_error_code(std::errc::invalid_argument); |
| 408 | |
| 409 | for (const DWARFUnitIndex::Entry &E : CUIndex.getRows()) { |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 410 | auto *I = E.getOffsets(); |
| 411 | if (!I) |
| 412 | continue; |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 413 | auto P = |
| 414 | IndexEntries.insert(std::make_pair(E.getSignature(), CurEntry)); |
| 415 | // FIXME: Check P.second and error for duplicate CU signatures |
| 416 | auto &NewEntry = P.first->second; |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 417 | for (auto Kind : CUIndex.getColumnKinds()) { |
| 418 | auto &C = NewEntry.Contributions[Kind - DW_SECT_INFO]; |
| 419 | C.Offset += I->Offset; |
| 420 | C.Length = I->Length; |
| 421 | ++I; |
| 422 | } |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 423 | } |
David Blaikie | 8bce5a0 | 2016-02-17 07:00:24 +0000 | [diff] [blame] | 424 | |
| 425 | if (!CurTypesSection.empty()) { |
| 426 | if (CurTUIndexSection.empty()) |
| 427 | return make_error_code(std::errc::invalid_argument); |
| 428 | DWARFUnitIndex TUIndex(DW_SECT_TYPES); |
| 429 | DataExtractor TUIndexData(CurTUIndexSection, |
| 430 | ErrOrObj->getBinary()->isLittleEndian(), 0); |
| 431 | if (!TUIndex.parse(TUIndexData)) |
| 432 | return make_error_code(std::errc::invalid_argument); |
| 433 | addAllTypesFromDWP(Out, TypeIndexEntries, TUIndex, TypesSection, |
| 434 | CurTypesSection, CurEntry, |
| 435 | ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); |
| 436 | } |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 437 | } else { |
David Blaikie | 852c02b | 2016-02-19 21:09:26 +0000 | [diff] [blame^] | 438 | IndexEntries.insert( |
| 439 | std::make_pair(getCUSignature(AbbrevSection, InfoSection), CurEntry)); |
| 440 | // FIXME: Check P.second and error for duplicate CU signatures |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 441 | addAllTypes(Out, TypeIndexEntries, TypesSection, CurTypesSection, |
| 442 | CurEntry, ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO]); |
David Blaikie | 2391937 | 2016-02-06 01:15:26 +0000 | [diff] [blame] | 443 | } |
David Blaikie | ad07b5d | 2015-12-04 17:20:04 +0000 | [diff] [blame] | 444 | |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame] | 445 | if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset, |
| 446 | StrSection, StrOffsetSection, |
| 447 | CurStrSection, CurStrOffsetSection)) |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 448 | return Err; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 449 | } |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 450 | |
David Blaikie | 9e51c84 | 2015-12-05 03:41:53 +0000 | [diff] [blame] | 451 | if (!TypeIndexEntries.empty()) { |
| 452 | // Lie about there being no info contributions so the TU index only includes |
| 453 | // the type unit contribution |
| 454 | ContributionOffsets[0] = 0; |
| 455 | writeIndex(Out, MCOFI.getDwarfTUIndexSection(), ContributionOffsets, |
| 456 | TypeIndexEntries); |
| 457 | } |
David Blaikie | b3757c0 | 2015-12-02 22:01:56 +0000 | [diff] [blame] | 458 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 459 | // Lie about the type contribution |
| 460 | ContributionOffsets[DW_SECT_TYPES - DW_SECT_INFO] = 0; |
| 461 | // Unlie about the info contribution |
| 462 | ContributionOffsets[0] = 1; |
David Blaikie | 7c4ffe0 | 2015-12-04 21:30:23 +0000 | [diff] [blame] | 463 | |
David Blaikie | 24c8ac9 | 2015-12-05 03:05:45 +0000 | [diff] [blame] | 464 | writeIndex(Out, MCOFI.getDwarfCUIndexSection(), ContributionOffsets, |
| 465 | IndexEntries); |
David Blaikie | b073cb9 | 2015-12-02 06:21:34 +0000 | [diff] [blame] | 466 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 467 | return std::error_code(); |
| 468 | } |
| 469 | |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 470 | int main(int argc, char **argv) { |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 471 | |
| 472 | ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files"); |
| 473 | |
| 474 | llvm::InitializeAllTargetInfos(); |
| 475 | llvm::InitializeAllTargetMCs(); |
| 476 | llvm::InitializeAllTargets(); |
| 477 | llvm::InitializeAllAsmPrinters(); |
| 478 | |
| 479 | std::string ErrorStr; |
| 480 | StringRef Context = "dwarf streamer init"; |
| 481 | |
| 482 | Triple TheTriple("x86_64-linux-gnu"); |
| 483 | |
| 484 | // Get the target. |
| 485 | const Target *TheTarget = |
| 486 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
| 487 | if (!TheTarget) |
| 488 | return error(ErrorStr, Context); |
| 489 | std::string TripleName = TheTriple.getTriple(); |
| 490 | |
| 491 | // Create all the MC Objects. |
| 492 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 493 | if (!MRI) |
| 494 | return error(Twine("no register info for target ") + TripleName, Context); |
| 495 | |
| 496 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 497 | if (!MAI) |
| 498 | return error("no asm info for target " + TripleName, Context); |
| 499 | |
| 500 | MCObjectFileInfo MOFI; |
| 501 | MCContext MC(MAI.get(), MRI.get(), &MOFI); |
David Blaikie | 2ed678c | 2015-12-05 03:06:30 +0000 | [diff] [blame] | 502 | MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, MC); |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 503 | |
| 504 | auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); |
| 505 | if (!MAB) |
| 506 | return error("no asm backend for target " + TripleName, Context); |
| 507 | |
| 508 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 509 | if (!MII) |
| 510 | return error("no instr info info for target " + TripleName, Context); |
| 511 | |
| 512 | std::unique_ptr<MCSubtargetInfo> MSTI( |
| 513 | TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 514 | if (!MSTI) |
| 515 | return error("no subtarget info for target " + TripleName, Context); |
| 516 | |
| 517 | MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC); |
| 518 | if (!MCE) |
| 519 | return error("no code emitter for target " + TripleName, Context); |
| 520 | |
| 521 | // Create the output file. |
| 522 | std::error_code EC; |
| 523 | raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None); |
| 524 | if (EC) |
| 525 | return error(Twine(OutputFilename) + ": " + EC.message(), Context); |
| 526 | |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 527 | MCTargetOptions MCOptions = InitMCTargetOptionsFromFlags(); |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 528 | std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( |
David Majnemer | 03e2cc3 | 2015-12-21 22:09:27 +0000 | [diff] [blame] | 529 | TheTriple, MC, *MAB, OutFile, MCE, *MSTI, MCOptions.MCRelaxAll, |
| 530 | MCOptions.MCIncrementalLinkerCompatible, |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 531 | /*DWARFMustBeAtTheEnd*/ false)); |
| 532 | if (!MS) |
| 533 | return error("no object streamer for target " + TripleName, Context); |
| 534 | |
| 535 | if (auto Err = write(*MS, InputFiles)) |
| 536 | return error(Err.message(), "Writing DWP file"); |
| 537 | |
| 538 | MS->Finish(); |
David Blaikie | df05525 | 2015-12-01 00:48:34 +0000 | [diff] [blame] | 539 | } |