1fe6060f1SDimitry Andric #ifndef LLVM_DWP_DWP_H
2fe6060f1SDimitry Andric #define LLVM_DWP_DWP_H
3fe6060f1SDimitry Andric 
4fe6060f1SDimitry Andric #include "DWPStringPool.h"
5fe6060f1SDimitry Andric #include "llvm/ADT/ArrayRef.h"
6fe6060f1SDimitry Andric #include "llvm/ADT/MapVector.h"
7fe6060f1SDimitry Andric #include "llvm/ADT/StringRef.h"
8fe6060f1SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFContext.h"
9fe6060f1SDimitry Andric #include "llvm/DebugInfo/DWARF/DWARFUnitIndex.h"
10fe6060f1SDimitry Andric #include "llvm/MC/MCSection.h"
11fe6060f1SDimitry Andric #include "llvm/MC/MCStreamer.h"
12fe6060f1SDimitry Andric #include "llvm/Object/ObjectFile.h"
13fe6060f1SDimitry Andric #include "llvm/Support/Error.h"
14fe6060f1SDimitry Andric #include <deque>
15fe6060f1SDimitry Andric #include <vector>
16fe6060f1SDimitry Andric 
17fe6060f1SDimitry Andric namespace llvm {
18*5f757f3fSDimitry Andric enum OnCuIndexOverflow {
19*5f757f3fSDimitry Andric   HardStop,
20*5f757f3fSDimitry Andric   SoftStop,
21*5f757f3fSDimitry Andric   Continue,
22*5f757f3fSDimitry Andric };
23*5f757f3fSDimitry Andric 
24fe6060f1SDimitry Andric struct UnitIndexEntry {
25fe6060f1SDimitry Andric   DWARFUnitIndex::Entry::SectionContribution Contributions[8];
26fe6060f1SDimitry Andric   std::string Name;
27fe6060f1SDimitry Andric   std::string DWOName;
28fe6060f1SDimitry Andric   StringRef DWPName;
29fe6060f1SDimitry Andric };
30fe6060f1SDimitry Andric 
31fe6060f1SDimitry Andric // Holds data for Skeleton, Split Compilation, and Type Unit Headers (only in
32fe6060f1SDimitry Andric // v5) as defined in Dwarf 5 specification, 7.5.1.2, 7.5.1.3 and Dwarf 4
33fe6060f1SDimitry Andric // specification 7.5.1.1.
34fe6060f1SDimitry Andric struct InfoSectionUnitHeader {
35fe6060f1SDimitry Andric   // unit_length field. Note that the type is uint64_t even in 32-bit dwarf.
36fe6060f1SDimitry Andric   uint64_t Length = 0;
37fe6060f1SDimitry Andric 
38fe6060f1SDimitry Andric   // version field.
39fe6060f1SDimitry Andric   uint16_t Version = 0;
40fe6060f1SDimitry Andric 
41fe6060f1SDimitry Andric   // unit_type field. Initialized only if Version >= 5.
42fe6060f1SDimitry Andric   uint8_t UnitType = 0;
43fe6060f1SDimitry Andric 
44fe6060f1SDimitry Andric   // address_size field.
45fe6060f1SDimitry Andric   uint8_t AddrSize = 0;
46fe6060f1SDimitry Andric 
47fe6060f1SDimitry Andric   // debug_abbrev_offset field. Note that the type is uint64_t even in 32-bit
48fe6060f1SDimitry Andric   // dwarf. It is assumed to be 0.
49fe6060f1SDimitry Andric   uint64_t DebugAbbrevOffset = 0;
50fe6060f1SDimitry Andric 
51fe6060f1SDimitry Andric   // dwo_id field. This resides in the header only if Version >= 5.
52fe6060f1SDimitry Andric   // In earlier versions, it is read from DW_AT_GNU_dwo_id.
53bdd1243dSDimitry Andric   std::optional<uint64_t> Signature;
54fe6060f1SDimitry Andric 
55fe6060f1SDimitry Andric   // Derived from the length of Length field.
56fe6060f1SDimitry Andric   dwarf::DwarfFormat Format = dwarf::DwarfFormat::DWARF32;
57fe6060f1SDimitry Andric 
58fe6060f1SDimitry Andric   // The size of the Header in bytes. This is derived while parsing the header,
59fe6060f1SDimitry Andric   // and is stored as a convenience.
60fe6060f1SDimitry Andric   uint8_t HeaderSize = 0;
61fe6060f1SDimitry Andric };
62fe6060f1SDimitry Andric 
63fe6060f1SDimitry Andric struct CompileUnitIdentifiers {
64fe6060f1SDimitry Andric   uint64_t Signature = 0;
65fe6060f1SDimitry Andric   const char *Name = "";
66fe6060f1SDimitry Andric   const char *DWOName = "";
67fe6060f1SDimitry Andric };
68fe6060f1SDimitry Andric 
6906c3fb27SDimitry Andric Error write(MCStreamer &Out, ArrayRef<std::string> Inputs,
70*5f757f3fSDimitry Andric             OnCuIndexOverflow OverflowOptValue);
71fe6060f1SDimitry Andric 
72fe6060f1SDimitry Andric unsigned getContributionIndex(DWARFSectionKind Kind, uint32_t IndexVersion);
73fe6060f1SDimitry Andric 
74fe6060f1SDimitry Andric Error handleSection(
75fe6060f1SDimitry Andric     const StringMap<std::pair<MCSection *, DWARFSectionKind>> &KnownSections,
76fe6060f1SDimitry Andric     const MCSection *StrSection, const MCSection *StrOffsetSection,
77fe6060f1SDimitry Andric     const MCSection *TypesSection, const MCSection *CUIndexSection,
78fe6060f1SDimitry Andric     const MCSection *TUIndexSection, const MCSection *InfoSection,
79fe6060f1SDimitry Andric     const object::SectionRef &Section, MCStreamer &Out,
80fe6060f1SDimitry Andric     std::deque<SmallString<32>> &UncompressedSections,
81fe6060f1SDimitry Andric     uint32_t (&ContributionOffsets)[8], UnitIndexEntry &CurEntry,
82fe6060f1SDimitry Andric     StringRef &CurStrSection, StringRef &CurStrOffsetSection,
83fe6060f1SDimitry Andric     std::vector<StringRef> &CurTypesSection,
84fe6060f1SDimitry Andric     std::vector<StringRef> &CurInfoSection, StringRef &AbbrevSection,
85fe6060f1SDimitry Andric     StringRef &CurCUIndexSection, StringRef &CurTUIndexSection,
86fe6060f1SDimitry Andric     std::vector<std::pair<DWARFSectionKind, uint32_t>> &SectionLength);
87fe6060f1SDimitry Andric 
88fe6060f1SDimitry Andric Expected<InfoSectionUnitHeader> parseInfoSectionUnitHeader(StringRef Info);
89fe6060f1SDimitry Andric 
90fe6060f1SDimitry Andric void writeStringsAndOffsets(MCStreamer &Out, DWPStringPool &Strings,
91fe6060f1SDimitry Andric                             MCSection *StrOffsetSection,
92fe6060f1SDimitry Andric                             StringRef CurStrSection,
93fe6060f1SDimitry Andric                             StringRef CurStrOffsetSection, uint16_t Version);
94fe6060f1SDimitry Andric 
95fe6060f1SDimitry Andric Error buildDuplicateError(const std::pair<uint64_t, UnitIndexEntry> &PrevE,
96fe6060f1SDimitry Andric                           const CompileUnitIdentifiers &ID, StringRef DWPName);
97fe6060f1SDimitry Andric 
98fe6060f1SDimitry Andric void writeIndex(MCStreamer &Out, MCSection *Section,
99fe6060f1SDimitry Andric                 ArrayRef<unsigned> ContributionOffsets,
100fe6060f1SDimitry Andric                 const MapVector<uint64_t, UnitIndexEntry> &IndexEntries,
101fe6060f1SDimitry Andric                 uint32_t IndexVersion);
102fe6060f1SDimitry Andric 
103fe6060f1SDimitry Andric } // namespace llvm
104fe6060f1SDimitry Andric #endif // LLVM_DWP_DWP_H
105