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" |
| 4 | #include "llvm/MC/MCAsmInfo.h" |
| 5 | #include "llvm/MC/MCContext.h" |
| 6 | #include "llvm/MC/MCInstrInfo.h" |
| 7 | #include "llvm/MC/MCObjectFileInfo.h" |
| 8 | #include "llvm/MC/MCRegisterInfo.h" |
| 9 | #include "llvm/MC/MCSectionELF.h" |
| 10 | #include "llvm/MC/MCStreamer.h" |
| 11 | #include "llvm/Object/ObjectFile.h" |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 12 | #include "llvm/Support/DataExtractor.h" |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 13 | #include "llvm/Support/Options.h" |
| 14 | #include "llvm/Support/FileSystem.h" |
| 15 | #include "llvm/Support/MemoryBuffer.h" |
| 16 | #include "llvm/Support/TargetRegistry.h" |
| 17 | #include "llvm/Support/raw_ostream.h" |
| 18 | #include "llvm/Target/TargetMachine.h" |
| 19 | #include "llvm/Support/TargetSelect.h" |
| 20 | #include <memory> |
| 21 | #include <list> |
| 22 | #include <unordered_set> |
| 23 | |
| 24 | using namespace llvm; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 25 | using namespace llvm::object; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 26 | using namespace cl; |
| 27 | |
| 28 | OptionCategory DwpCategory("Specific Options"); |
| 29 | static list<std::string> InputFiles(Positional, OneOrMore, |
| 30 | desc("<input files>"), cat(DwpCategory)); |
| 31 | |
| 32 | static opt<std::string> OutputFilename(Required, "o", desc("Specify the output file."), |
| 33 | value_desc("filename"), cat(DwpCategory)); |
| 34 | |
| 35 | static int error(const Twine &Error, const Twine &Context) { |
| 36 | errs() << Twine("while processing ") + Context + ":\n"; |
| 37 | errs() << Twine("error: ") + Error + "\n"; |
| 38 | return 1; |
| 39 | } |
| 40 | |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 41 | static std::error_code |
| 42 | writeStringsAndOffsets(MCStreamer &Out, StringMap<uint32_t> &Strings, |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame^] | 43 | uint32_t &StringOffset, MCSection *StrSection, |
| 44 | MCSection *StrOffsetSection, StringRef CurStrSection, |
| 45 | StringRef CurStrOffsetSection) { |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 46 | // Could possibly produce an error or warning if one of these was non-null but |
| 47 | // the other was null. |
| 48 | if (CurStrSection.empty() || CurStrOffsetSection.empty()) |
| 49 | return std::error_code(); |
| 50 | |
| 51 | DenseMap<uint32_t, uint32_t> OffsetRemapping; |
| 52 | |
| 53 | DataExtractor Data(CurStrSection, true, 0); |
| 54 | uint32_t LocalOffset = 0; |
| 55 | uint32_t PrevOffset = 0; |
| 56 | while (const char *s = Data.getCStr(&LocalOffset)) { |
| 57 | StringRef Str(s, LocalOffset - PrevOffset - 1); |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame^] | 58 | auto Pair = Strings.insert(std::make_pair(Str, StringOffset)); |
| 59 | if (Pair.second) { |
| 60 | Out.SwitchSection(StrSection); |
| 61 | Out.EmitBytes( |
| 62 | StringRef(Pair.first->getKeyData(), Pair.first->getKeyLength() + 1)); |
| 63 | StringOffset += Str.size() + 1; |
| 64 | } |
| 65 | OffsetRemapping[PrevOffset] = Pair.first->second; |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 66 | PrevOffset = LocalOffset; |
| 67 | } |
| 68 | |
| 69 | Data = DataExtractor(CurStrOffsetSection, true, 0); |
| 70 | |
| 71 | Out.SwitchSection(StrOffsetSection); |
| 72 | |
| 73 | uint32_t Offset = 0; |
| 74 | uint64_t Size = CurStrOffsetSection.size(); |
| 75 | while (Offset < Size) { |
| 76 | auto OldOffset = Data.getU32(&Offset); |
| 77 | auto NewOffset = OffsetRemapping[OldOffset]; |
| 78 | Out.EmitIntValue(NewOffset, 4); |
| 79 | } |
| 80 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 81 | return std::error_code(); |
| 82 | } |
| 83 | |
| 84 | static std::error_code write(MCStreamer &Out, ArrayRef<std::string> Inputs) { |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 85 | const auto &MCOFI = *Out.getContext().getObjectFileInfo(); |
| 86 | MCSection *const StrSection = MCOFI.getDwarfStrDWOSection(); |
| 87 | MCSection *const StrOffsetSection = MCOFI.getDwarfStrOffDWOSection(); |
| 88 | const StringMap<MCSection *> KnownSections = { |
| 89 | {"debug_info.dwo", MCOFI.getDwarfInfoDWOSection()}, |
| 90 | {"debug_types.dwo", MCOFI.getDwarfTypesDWOSection()}, |
| 91 | {"debug_str_offsets.dwo", StrOffsetSection}, |
| 92 | {"debug_str.dwo", StrSection}, |
| 93 | {"debug_loc.dwo", MCOFI.getDwarfLocDWOSection()}, |
| 94 | {"debug_abbrev.dwo", MCOFI.getDwarfAbbrevDWOSection()}}; |
| 95 | |
| 96 | StringMap<uint32_t> Strings; |
| 97 | uint32_t StringOffset = 0; |
| 98 | |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 99 | for (const auto &Input : Inputs) { |
| 100 | auto ErrOrObj = object::ObjectFile::createObjectFile(Input); |
| 101 | if (!ErrOrObj) |
| 102 | return ErrOrObj.getError(); |
| 103 | const auto *Obj = ErrOrObj->getBinary(); |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 104 | StringRef CurStrSection; |
| 105 | StringRef CurStrOffsetSection; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 106 | for (const auto &Section : Obj->sections()) { |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 107 | StringRef Name; |
| 108 | if (std::error_code Err = Section.getName(Name)) |
| 109 | return Err; |
| 110 | if (MCSection *OutSection = |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 111 | KnownSections.lookup(Name.substr(Name.find_first_not_of("._")))) { |
| 112 | StringRef Contents; |
| 113 | if (auto Err = Section.getContents(Contents)) |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 114 | return Err; |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame^] | 115 | if (OutSection == StrOffsetSection) |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 116 | CurStrOffsetSection = Contents; |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame^] | 117 | else if (OutSection == StrSection) |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 118 | CurStrSection = Contents; |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame^] | 119 | else { |
| 120 | Out.SwitchSection(OutSection); |
| 121 | Out.EmitBytes(Contents); |
| 122 | } |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 123 | } |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 124 | } |
David Blaikie | bb94e44 | 2015-12-01 19:17:58 +0000 | [diff] [blame^] | 125 | if (auto Err = writeStringsAndOffsets(Out, Strings, StringOffset, |
| 126 | StrSection, StrOffsetSection, |
| 127 | CurStrSection, CurStrOffsetSection)) |
David Blaikie | 98ad82a | 2015-12-01 18:07:07 +0000 | [diff] [blame] | 128 | return Err; |
David Blaikie | 242b948 | 2015-12-01 00:48:39 +0000 | [diff] [blame] | 129 | } |
| 130 | return std::error_code(); |
| 131 | } |
| 132 | |
| 133 | int main(int argc, char** argv) { |
| 134 | |
| 135 | ParseCommandLineOptions(argc, argv, "merge split dwarf (.dwo) files"); |
| 136 | |
| 137 | llvm::InitializeAllTargetInfos(); |
| 138 | llvm::InitializeAllTargetMCs(); |
| 139 | llvm::InitializeAllTargets(); |
| 140 | llvm::InitializeAllAsmPrinters(); |
| 141 | |
| 142 | std::string ErrorStr; |
| 143 | StringRef Context = "dwarf streamer init"; |
| 144 | |
| 145 | Triple TheTriple("x86_64-linux-gnu"); |
| 146 | |
| 147 | // Get the target. |
| 148 | const Target *TheTarget = |
| 149 | TargetRegistry::lookupTarget("", TheTriple, ErrorStr); |
| 150 | if (!TheTarget) |
| 151 | return error(ErrorStr, Context); |
| 152 | std::string TripleName = TheTriple.getTriple(); |
| 153 | |
| 154 | // Create all the MC Objects. |
| 155 | std::unique_ptr<MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TripleName)); |
| 156 | if (!MRI) |
| 157 | return error(Twine("no register info for target ") + TripleName, Context); |
| 158 | |
| 159 | std::unique_ptr<MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TripleName)); |
| 160 | if (!MAI) |
| 161 | return error("no asm info for target " + TripleName, Context); |
| 162 | |
| 163 | MCObjectFileInfo MOFI; |
| 164 | MCContext MC(MAI.get(), MRI.get(), &MOFI); |
| 165 | MOFI.InitMCObjectFileInfo(TheTriple, Reloc::Default, CodeModel::Default, |
| 166 | MC); |
| 167 | |
| 168 | auto MAB = TheTarget->createMCAsmBackend(*MRI, TripleName, ""); |
| 169 | if (!MAB) |
| 170 | return error("no asm backend for target " + TripleName, Context); |
| 171 | |
| 172 | std::unique_ptr<MCInstrInfo> MII(TheTarget->createMCInstrInfo()); |
| 173 | if (!MII) |
| 174 | return error("no instr info info for target " + TripleName, Context); |
| 175 | |
| 176 | std::unique_ptr<MCSubtargetInfo> MSTI( |
| 177 | TheTarget->createMCSubtargetInfo(TripleName, "", "")); |
| 178 | if (!MSTI) |
| 179 | return error("no subtarget info for target " + TripleName, Context); |
| 180 | |
| 181 | MCCodeEmitter *MCE = TheTarget->createMCCodeEmitter(*MII, *MRI, MC); |
| 182 | if (!MCE) |
| 183 | return error("no code emitter for target " + TripleName, Context); |
| 184 | |
| 185 | // Create the output file. |
| 186 | std::error_code EC; |
| 187 | raw_fd_ostream OutFile(OutputFilename, EC, sys::fs::F_None); |
| 188 | if (EC) |
| 189 | return error(Twine(OutputFilename) + ": " + EC.message(), Context); |
| 190 | |
| 191 | std::unique_ptr<MCStreamer> MS(TheTarget->createMCObjectStreamer( |
| 192 | TheTriple, MC, *MAB, OutFile, MCE, *MSTI, false, |
| 193 | /*DWARFMustBeAtTheEnd*/ false)); |
| 194 | if (!MS) |
| 195 | return error("no object streamer for target " + TripleName, Context); |
| 196 | |
| 197 | if (auto Err = write(*MS, InputFiles)) |
| 198 | return error(Err.message(), "Writing DWP file"); |
| 199 | |
| 200 | MS->Finish(); |
David Blaikie | df05525 | 2015-12-01 00:48:34 +0000 | [diff] [blame] | 201 | } |