llvm-dwp.cpp 28 KB

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