1313#include " bolt/Core/DebugData.h"
1414#include " llvm/ADT/StringRef.h"
1515#include " llvm/CodeGen/DIE.h"
16+ #include " llvm/DWP/DWP.h"
1617#include " llvm/MC/MCAsmLayout.h"
18+ #include " llvm/MC/MCContext.h"
19+ #include " llvm/Support/ToolOutputFile.h"
1720#include < cstdint>
1821#include < memory>
1922#include < mutex>
@@ -45,6 +48,7 @@ class DWARFRewriter {
4548 uint64_t Length;
4649 uint64_t TUHash;
4750 };
51+ using UnitMetaVectorType = std::vector<UnitMeta>;
4852
4953private:
5054 BinaryContext &BC;
@@ -88,15 +92,6 @@ class DWARFRewriter {
8892
8993 std::mutex LocListDebugInfoPatchesMutex;
9094
91- // / Dwo id specific its .debug_info.dwo section content.
92- std::unordered_map<uint64_t , std::string> DwoDebugInfoMap;
93-
94- // / Dwo id specific its .debug_abbrev.dwo section content.
95- std::unordered_map<uint64_t , std::string> DwoDebugAbbrevMap;
96-
97- // / Dwo id specific its .debug_types.dwo section content.
98- std::unordered_map<uint64_t , std::string> DwoDebugTypeMap;
99-
10095 // / Dwo id specific its RangesBase.
10196 std::unordered_map<uint64_t , uint64_t > DwoRangesBase;
10297
@@ -107,12 +102,6 @@ class DWARFRewriter {
107102 using GDBIndexTUEntryType = std::vector<GDBIndexTUEntry>;
108103 GDBIndexTUEntryType GDBIndexTUEntryVector;
109104
110- using UnitMetaVectorType = std::vector<UnitMeta>;
111- using TUnitMetaDwoMapType = std::unordered_map<uint64_t , UnitMetaVectorType>;
112- using CUnitMetaDwoMapType = std::unordered_map<uint64_t , UnitMeta>;
113- CUnitMetaDwoMapType CUnitMetaDwoMap;
114- TUnitMetaDwoMapType TUnitMetaDwoMap;
115-
116105 // / DWARFLegacy is all DWARF versions before DWARF 5.
117106 enum class DWARFVersion { DWARFLegacy, DWARF5 };
118107
@@ -149,12 +138,6 @@ class DWARFRewriter {
149138 // / Rewrite .gdb_index section if present.
150139 void updateGdbIndexSection (CUOffsetMap &CUMap, uint32_t NumCUs);
151140
152- // / Output .dwo files.
153- void writeDWOFiles (std::unordered_map<uint64_t , std::string> &DWOIdToName);
154-
155- // / Output .dwp files.
156- void writeDWP (std::unordered_map<uint64_t , std::string> &DWOIdToName);
157-
158141 // / DWARFDie contains a pointer to a DIE and hence gets invalidated once the
159142 // / embedded DIE is destroyed. This wrapper class stores a DIE internally and
160143 // / could be cast to a DWARFDie that is valid even after the initial DIE is
@@ -197,39 +180,8 @@ class DWARFRewriter {
197180 // / Update stmt_list for CUs based on the new .debug_line \p Layout.
198181 void updateLineTableOffsets (const MCAsmLayout &Layout);
199182
200- // / Given a \p DWOId, return its DebugLocWriter if it exists.
201- DebugLocWriter *getDebugLocWriter (uint64_t DWOId) {
202- auto Iter = LocListWritersByCU.find (DWOId);
203- return Iter == LocListWritersByCU.end () ? nullptr
204- : LocListWritersByCU[DWOId].get ();
205- }
206-
207- StringRef getDwoDebugInfoStr (uint64_t DWOId) {
208- return DwoDebugInfoMap[DWOId];
209- }
210-
211- StringRef getDwoDebugAbbrevStr (uint64_t DWOId) {
212- return DwoDebugAbbrevMap[DWOId];
213- }
214-
215- StringRef getDwoDebugTypeStr (uint64_t DWOId) {
216- return DwoDebugTypeMap[DWOId];
217- }
218-
219183 uint64_t getDwoRangesBase (uint64_t DWOId) { return DwoRangesBase[DWOId]; }
220184
221- void setDwoDebugInfoStr (uint64_t DWOId, StringRef Str) {
222- DwoDebugInfoMap[DWOId] = Str.str ();
223- }
224-
225- void setDwoDebugAbbrevStr (uint64_t DWOId, StringRef Str) {
226- DwoDebugAbbrevMap[DWOId] = Str.str ();
227- }
228-
229- void setDwoDebugTypeStr (uint64_t DWOId, StringRef Str) {
230- DwoDebugTypeMap[DWOId] = Str.str ();
231- }
232-
233185 void setDwoRangesBase (uint64_t DWOId, uint64_t RangesBase) {
234186 DwoRangesBase[DWOId] = RangesBase;
235187 }
@@ -242,30 +194,37 @@ class DWARFRewriter {
242194 return GDBIndexTUEntryVector;
243195 }
244196
245- // / Stores meta data for each CU per DWO ID. It's used to create cu-index for
246- // / DWARF5.
247- void addCUnitMetaEntry (const uint64_t DWOId, const UnitMeta &Entry) {
248- auto RetVal = CUnitMetaDwoMap.insert ({DWOId, Entry});
249- if (!RetVal.second )
250- errs () << " BOLT-WARNING: [internal-dwarf-error]: Trying to set CU meta "
251- " data twice for DWOID: "
252- << Twine::utohexstr (DWOId) << " .\n " ;
253- }
197+ using OverriddenSectionsMap = std::unordered_map<DWARFSectionKind, StringRef>;
198+ // / Output .dwo files.
199+ void writeDWOFiles (DWARFUnit &, const OverriddenSectionsMap &,
200+ const std::string &, DebugLocWriter &);
201+ using KnownSectionsEntry = std::pair<MCSection *, DWARFSectionKind>;
202+ struct DWPState {
203+ std::unique_ptr<ToolOutputFile> Out;
204+ std::unique_ptr<BinaryContext> TmpBC;
205+ std::unique_ptr<MCStreamer> Streamer;
206+ std::unique_ptr<DWPStringPool> Strings;
207+ const MCObjectFileInfo *MCOFI = nullptr ;
208+ const DWARFUnitIndex *CUIndex = nullptr ;
209+ std::deque<SmallString<32 >> UncompressedSections;
210+ MapVector<uint64_t , UnitIndexEntry> IndexEntries;
211+ MapVector<uint64_t , UnitIndexEntry> TypeIndexEntries;
212+ StringMap<KnownSectionsEntry> KnownSections;
213+ uint32_t ContributionOffsets[8 ] = {};
214+ uint32_t IndexVersion = 2 ;
215+ uint64_t DebugInfoSize = 0 ;
216+ uint16_t Version = 0 ;
217+ bool IsDWP = false ;
218+ };
219+ // / Init .dwp file
220+ void initDWPState (DWPState &);
254221
255- // / Stores meta data for each TU per DWO ID. It's used to create cu-index for
256- // / DWARF5.
257- void addTUnitMetaEntry (const uint64_t DWOId, const UnitMeta &Entry) {
258- TUnitMetaDwoMap[DWOId].emplace_back (Entry);
259- }
222+ // / Write out .dwp File
223+ void finalizeDWP (DWPState &);
260224
261- // / Returns Meta data for TUs in offset increasing order.
262- UnitMetaVectorType &getTUnitMetaEntries (const uint64_t DWOId) {
263- return TUnitMetaDwoMap[DWOId];
264- }
265- // / Returns Meta data for TUs in offset increasing order.
266- const UnitMeta &getCUnitMetaEntry (const uint64_t DWOId) {
267- return CUnitMetaDwoMap[DWOId];
268- }
225+ // / add content of dwo to .dwp file.
226+ void updateDWP (DWARFUnit &, const OverriddenSectionsMap &, const UnitMeta &,
227+ UnitMetaVectorType &, DWPState &, DebugLocWriter &);
269228};
270229
271230} // namespace bolt
0 commit comments