1 //===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief The DWARF component of yaml2obj. Provided as library code for tests.
13 //===----------------------------------------------------------------------===//
15 #include "llvm/ObjectYAML/DWARFEmitter.h"
16 #include "llvm/ObjectYAML/DWARFYAML.h"
17 #include "llvm/Support/Error.h"
18 #include "llvm/Support/LEB128.h"
19 #include "llvm/Support/SwapByteOrder.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include "DWARFVisitor.h"
29 static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
30 if (IsLittleEndian != sys::IsLittleEndianHost)
31 sys::swapByteOrder(Integer);
32 OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
35 static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
36 raw_ostream &OS, bool IsLittleEndian) {
38 writeInteger((uint64_t)Integer, OS, IsLittleEndian);
40 writeInteger((uint32_t)Integer, OS, IsLittleEndian);
42 writeInteger((uint16_t)Integer, OS, IsLittleEndian);
44 writeInteger((uint8_t)Integer, OS, IsLittleEndian);
46 assert(false && "Invalid integer write size.");
49 static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
50 std::vector<uint8_t> FillData;
51 FillData.insert(FillData.begin(), Size, 0);
52 OS.write(reinterpret_cast<char *>(FillData.data()), Size);
55 void writeInitialLength(const DWARFYAML::InitialLength &Length, raw_ostream &OS,
56 bool IsLittleEndian) {
57 writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian);
58 if (Length.isDWARF64())
59 writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
62 void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
63 for (auto Str : DI.DebugStrings) {
64 OS.write(Str.data(), Str.size());
69 void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
70 for (auto AbbrevDecl : DI.AbbrevDecls) {
71 encodeULEB128(AbbrevDecl.Code, OS);
72 encodeULEB128(AbbrevDecl.Tag, OS);
73 OS.write(AbbrevDecl.Children);
74 for (auto Attr : AbbrevDecl.Attributes) {
75 encodeULEB128(Attr.Attribute, OS);
76 encodeULEB128(Attr.Form, OS);
77 if (Attr.Form == dwarf::DW_FORM_implicit_const)
78 encodeSLEB128(Attr.Value, OS);
85 void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
86 for (auto Range : DI.ARanges) {
87 auto HeaderStart = OS.tell();
88 writeInitialLength(Range.Length, OS, DI.IsLittleEndian);
89 writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
90 writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
91 writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
92 writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
94 auto HeaderSize = OS.tell() - HeaderStart;
95 auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
96 ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
98 for (auto Descriptor : Range.Descriptors) {
99 writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
101 writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
104 ZeroFillBytes(OS, Range.AddrSize * 2);
108 void DWARFYAML::EmitPubSection(raw_ostream &OS,
109 const DWARFYAML::PubSection &Sect,
110 bool IsLittleEndian) {
111 writeInitialLength(Sect.Length, OS, IsLittleEndian);
112 writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
113 writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
114 writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
115 for (auto Entry : Sect.Entries) {
116 writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
118 writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
119 OS.write(Entry.Name.data(), Entry.Name.size());
124 /// \brief An extension of the DWARFYAML::ConstVisitor which writes compile
125 /// units and DIEs to a stream.
126 class DumpVisitor : public DWARFYAML::ConstVisitor {
130 virtual void onStartCompileUnit(const DWARFYAML::Unit &CU) {
131 writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
132 writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
133 if(CU.Version >= 5) {
134 writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
135 writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
136 writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
138 writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
139 writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
144 virtual void onStartDIE(const DWARFYAML::Unit &CU,
145 const DWARFYAML::Entry &DIE) {
146 encodeULEB128(DIE.AbbrCode, OS);
149 virtual void onValue(const uint8_t U) {
150 writeInteger(U, OS, DebugInfo.IsLittleEndian);
153 virtual void onValue(const uint16_t U) {
154 writeInteger(U, OS, DebugInfo.IsLittleEndian);
156 virtual void onValue(const uint32_t U) {
157 writeInteger(U, OS, DebugInfo.IsLittleEndian);
159 virtual void onValue(const uint64_t U, const bool LEB = false) {
161 encodeULEB128(U, OS);
163 writeInteger(U, OS, DebugInfo.IsLittleEndian);
166 virtual void onValue(const int64_t S, const bool LEB = false) {
168 encodeSLEB128(S, OS);
170 writeInteger(S, OS, DebugInfo.IsLittleEndian);
173 virtual void onValue(const StringRef String) {
174 OS.write(String.data(), String.size());
178 virtual void onValue(const MemoryBufferRef MBR) {
179 OS.write(MBR.getBufferStart(), MBR.getBufferSize());
183 DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out)
184 : DWARFYAML::ConstVisitor(DI), OS(Out) {}
187 void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
188 DumpVisitor Visitor(DI, OS);
189 Visitor.traverseDebugInfo();
192 static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
193 OS.write(File.Name.data(), File.Name.size());
195 encodeULEB128(File.DirIdx, OS);
196 encodeULEB128(File.ModTime, OS);
197 encodeULEB128(File.Length, OS);
200 void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
201 for (const auto &LineTable : DI.DebugLines) {
202 writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian);
203 uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
204 writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
205 writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
206 OS, DI.IsLittleEndian);
207 writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
208 if (LineTable.Version >= 4)
209 writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
210 writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
211 writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
212 writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
213 writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
215 for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
216 writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
218 for (auto IncludeDir : LineTable.IncludeDirs) {
219 OS.write(IncludeDir.data(), IncludeDir.size());
224 for (auto File : LineTable.Files)
225 EmitFileEntry(OS, File);
228 for (auto Op : LineTable.Opcodes) {
229 writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
230 if (Op.Opcode == 0) {
231 encodeULEB128(Op.ExtLen, OS);
232 writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
233 switch (Op.SubOpcode) {
234 case dwarf::DW_LNE_set_address:
235 case dwarf::DW_LNE_set_discriminator:
236 writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
239 case dwarf::DW_LNE_define_file:
240 EmitFileEntry(OS, Op.FileEntry);
242 case dwarf::DW_LNE_end_sequence:
245 for (auto OpByte : Op.UnknownOpcodeData)
246 writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
248 } else if (Op.Opcode < LineTable.OpcodeBase) {
250 case dwarf::DW_LNS_copy:
251 case dwarf::DW_LNS_negate_stmt:
252 case dwarf::DW_LNS_set_basic_block:
253 case dwarf::DW_LNS_const_add_pc:
254 case dwarf::DW_LNS_set_prologue_end:
255 case dwarf::DW_LNS_set_epilogue_begin:
258 case dwarf::DW_LNS_advance_pc:
259 case dwarf::DW_LNS_set_file:
260 case dwarf::DW_LNS_set_column:
261 case dwarf::DW_LNS_set_isa:
262 encodeULEB128(Op.Data, OS);
265 case dwarf::DW_LNS_advance_line:
266 encodeSLEB128(Op.SData, OS);
269 case dwarf::DW_LNS_fixed_advance_pc:
270 writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
274 for (auto OpData : Op.StandardOpcodeData) {
275 encodeULEB128(OpData, OS);
283 typedef void (*EmitFuncType)(raw_ostream &, const DWARFYAML::Data &);
286 EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
288 StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
290 raw_string_ostream DebugInfoStream(Data);
291 EmitFunc(DebugInfoStream, DI);
292 DebugInfoStream.flush();
294 OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
297 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
298 DWARFYAML::EmitDebugSections(StringRef YAMLString,
299 bool IsLittleEndian) {
300 StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
302 yaml::Input YIn(YAMLString);
305 DI.IsLittleEndian = IsLittleEndian;
308 return errorCodeToError(YIn.error());
310 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
312 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
314 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
316 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
318 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
320 return std::move(DebugSections);