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