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