LLVM  5.0.0
DWARFEmitter.cpp
Go to the documentation of this file.
1 //===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 ///
10 /// \file
11 /// \brief The DWARF component of yaml2obj. Provided as library code for tests.
12 ///
13 //===----------------------------------------------------------------------===//
14 
16 #include "DWARFVisitor.h"
17 #include "llvm/ADT/StringMap.h"
18 #include "llvm/ADT/StringRef.h"
20 #include "llvm/Support/Error.h"
21 #include "llvm/Support/Host.h"
22 #include "llvm/Support/LEB128.h"
28 #include <algorithm>
29 #include <cassert>
30 #include <cstddef>
31 #include <cstdint>
32 #include <memory>
33 #include <string>
34 #include <vector>
35 
36 using namespace llvm;
37 
38 template <typename T>
39 static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
40  if (IsLittleEndian != sys::IsLittleEndianHost)
41  sys::swapByteOrder(Integer);
42  OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
43 }
44 
45 static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
46  raw_ostream &OS, bool IsLittleEndian) {
47  if (8 == Size)
48  writeInteger((uint64_t)Integer, OS, IsLittleEndian);
49  else if (4 == Size)
50  writeInteger((uint32_t)Integer, OS, IsLittleEndian);
51  else if (2 == Size)
52  writeInteger((uint16_t)Integer, OS, IsLittleEndian);
53  else if (1 == Size)
54  writeInteger((uint8_t)Integer, OS, IsLittleEndian);
55  else
56  assert(false && "Invalid integer write size.");
57 }
58 
59 static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
60  std::vector<uint8_t> FillData;
61  FillData.insert(FillData.begin(), Size, 0);
62  OS.write(reinterpret_cast<char *>(FillData.data()), Size);
63 }
64 
66  bool IsLittleEndian) {
67  writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian);
68  if (Length.isDWARF64())
69  writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
70 }
71 
73  for (auto Str : DI.DebugStrings) {
74  OS.write(Str.data(), Str.size());
75  OS.write('\0');
76  }
77 }
78 
80  for (auto AbbrevDecl : DI.AbbrevDecls) {
81  encodeULEB128(AbbrevDecl.Code, OS);
82  encodeULEB128(AbbrevDecl.Tag, OS);
83  OS.write(AbbrevDecl.Children);
84  for (auto Attr : AbbrevDecl.Attributes) {
85  encodeULEB128(Attr.Attribute, OS);
86  encodeULEB128(Attr.Form, OS);
87  if (Attr.Form == dwarf::DW_FORM_implicit_const)
88  encodeSLEB128(Attr.Value, OS);
89  }
90  encodeULEB128(0, OS);
91  encodeULEB128(0, OS);
92  }
93 }
94 
96  for (auto Range : DI.ARanges) {
97  auto HeaderStart = OS.tell();
98  writeInitialLength(Range.Length, OS, DI.IsLittleEndian);
99  writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
100  writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
101  writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
102  writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
103 
104  auto HeaderSize = OS.tell() - HeaderStart;
105  auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
106  ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
107 
108  for (auto Descriptor : Range.Descriptors) {
109  writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
110  DI.IsLittleEndian);
111  writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
112  DI.IsLittleEndian);
113  }
114  ZeroFillBytes(OS, Range.AddrSize * 2);
115  }
116 }
117 
119  const DWARFYAML::PubSection &Sect,
120  bool IsLittleEndian) {
121  writeInitialLength(Sect.Length, OS, IsLittleEndian);
122  writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
123  writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
124  writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
125  for (auto Entry : Sect.Entries) {
126  writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
127  if (Sect.IsGNUStyle)
128  writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
129  OS.write(Entry.Name.data(), Entry.Name.size());
130  OS.write('\0');
131  }
132 }
133 
134 /// \brief An extension of the DWARFYAML::ConstVisitor which writes compile
135 /// units and DIEs to a stream.
137  raw_ostream &OS;
138 
139 protected:
140  void onStartCompileUnit(const DWARFYAML::Unit &CU) override {
141  writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
142  writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
143  if(CU.Version >= 5) {
144  writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
145  writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
146  writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
147  }else {
148  writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
149  writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
150  }
151 
152  }
153 
154  void onStartDIE(const DWARFYAML::Unit &CU,
155  const DWARFYAML::Entry &DIE) override {
156  encodeULEB128(DIE.AbbrCode, OS);
157  }
158 
159  void onValue(const uint8_t U) override {
160  writeInteger(U, OS, DebugInfo.IsLittleEndian);
161  }
162 
163  void onValue(const uint16_t U) override {
164  writeInteger(U, OS, DebugInfo.IsLittleEndian);
165  }
166 
167  void onValue(const uint32_t U) override {
168  writeInteger(U, OS, DebugInfo.IsLittleEndian);
169  }
170 
171  void onValue(const uint64_t U, const bool LEB = false) override {
172  if (LEB)
173  encodeULEB128(U, OS);
174  else
175  writeInteger(U, OS, DebugInfo.IsLittleEndian);
176  }
177 
178  void onValue(const int64_t S, const bool LEB = false) override {
179  if (LEB)
180  encodeSLEB128(S, OS);
181  else
182  writeInteger(S, OS, DebugInfo.IsLittleEndian);
183  }
184 
185  void onValue(const StringRef String) override {
186  OS.write(String.data(), String.size());
187  OS.write('\0');
188  }
189 
190  void onValue(const MemoryBufferRef MBR) override {
191  OS.write(MBR.getBufferStart(), MBR.getBufferSize());
192  }
193 
194 public:
196  : DWARFYAML::ConstVisitor(DI), OS(Out) {}
197 };
198 
200  DumpVisitor Visitor(DI, OS);
201  Visitor.traverseDebugInfo();
202 }
203 
204 static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
205  OS.write(File.Name.data(), File.Name.size());
206  OS.write('\0');
207  encodeULEB128(File.DirIdx, OS);
208  encodeULEB128(File.ModTime, OS);
209  encodeULEB128(File.Length, OS);
210 }
211 
213  for (const auto &LineTable : DI.DebugLines) {
215  uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
216  writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
217  writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
218  OS, DI.IsLittleEndian);
220  if (LineTable.Version >= 4)
223  writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
226 
227  for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
228  writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
229 
230  for (auto IncludeDir : LineTable.IncludeDirs) {
231  OS.write(IncludeDir.data(), IncludeDir.size());
232  OS.write('\0');
233  }
234  OS.write('\0');
235 
236  for (auto File : LineTable.Files)
237  EmitFileEntry(OS, File);
238  OS.write('\0');
239 
240  for (auto Op : LineTable.Opcodes) {
241  writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
242  if (Op.Opcode == 0) {
243  encodeULEB128(Op.ExtLen, OS);
244  writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
245  switch (Op.SubOpcode) {
246  case dwarf::DW_LNE_set_address:
247  case dwarf::DW_LNE_set_discriminator:
248  writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
249  DI.IsLittleEndian);
250  break;
251  case dwarf::DW_LNE_define_file:
252  EmitFileEntry(OS, Op.FileEntry);
253  break;
254  case dwarf::DW_LNE_end_sequence:
255  break;
256  default:
257  for (auto OpByte : Op.UnknownOpcodeData)
258  writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
259  }
260  } else if (Op.Opcode < LineTable.OpcodeBase) {
261  switch (Op.Opcode) {
262  case dwarf::DW_LNS_copy:
263  case dwarf::DW_LNS_negate_stmt:
264  case dwarf::DW_LNS_set_basic_block:
265  case dwarf::DW_LNS_const_add_pc:
266  case dwarf::DW_LNS_set_prologue_end:
267  case dwarf::DW_LNS_set_epilogue_begin:
268  break;
269 
270  case dwarf::DW_LNS_advance_pc:
271  case dwarf::DW_LNS_set_file:
272  case dwarf::DW_LNS_set_column:
273  case dwarf::DW_LNS_set_isa:
274  encodeULEB128(Op.Data, OS);
275  break;
276 
277  case dwarf::DW_LNS_advance_line:
278  encodeSLEB128(Op.SData, OS);
279  break;
280 
281  case dwarf::DW_LNS_fixed_advance_pc:
282  writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
283  break;
284 
285  default:
286  for (auto OpData : Op.StandardOpcodeData) {
287  encodeULEB128(OpData, OS);
288  }
289  }
290  }
291  }
292  }
293 }
294 
295 using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &);
296 
297 static void
299  StringRef Sec,
300  StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
301  std::string Data;
302  raw_string_ostream DebugInfoStream(Data);
303  EmitFunc(DebugInfoStream, DI);
304  DebugInfoStream.flush();
305  if (!Data.empty())
306  OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
307 }
308 
311  bool IsLittleEndian) {
313 
314  yaml::Input YIn(YAMLString);
315 
316  DWARFYAML::Data DI;
317  DI.IsLittleEndian = IsLittleEndian;
318  YIn >> DI;
319  if (YIn.error())
320  return errorCodeToError(YIn.error());
321 
323  DebugSections);
325  DebugSections);
327  DebugSections);
329  DebugSections);
331  DebugSections);
332  return std::move(DebugSections);
333 }
std::error_code error()
Definition: YAMLTraits.cpp:73
void swapByteOrder(T &Value)
void EmitDebugInfo(raw_ostream &OS, const Data &DI)
llvm::dwarf::UnitType Type
Definition: DWARFYAML.h:104
void EmitDebugAranges(raw_ostream &OS, const Data &DI)
std::vector< Abbrev > AbbrevDecls
Definition: DWARFYAML.h:146
const char * getBufferStart() const
Definition: MemoryBuffer.h:173
uint64_t alignTo(uint64_t Value, uint64_t Align, uint64_t Skew=0)
Returns the next integer (mod 2**64) that is greater than or equal to Value and is a multiple of Alig...
Definition: MathExtras.h:677
static void ZeroFillBytes(raw_ostream &OS, size_t Size)
std::vector< uint8_t > StandardOpcodeLengths
Definition: DWARFYAML.h:138
std::vector< PubEntry > Entries
Definition: DWARFYAML.h:87
DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out)
static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian)
An extension of the DWARFYAML::ConstVisitor which writes compile units and DIEs to a stream...
Tagged union holding either a T or a Error.
Definition: CachePruning.h:23
void onValue(const MemoryBufferRef MBR) override
The Input class is used to parse a yaml document into in-memory structs and vectors.
Definition: YAMLTraits.h:1108
uint64_t tell() const
tell - Return the current offset with the file.
Definition: raw_ostream.h:98
std::vector< ARange > ARanges
Definition: DWARFYAML.h:148
void EmitDebugAbbrev(raw_ostream &OS, const Data &DI)
static const bool IsLittleEndianHost
Definition: Host.h:51
Expected< StringMap< std::unique_ptr< MemoryBuffer > > > EmitDebugSections(StringRef YAMLString, bool IsLittleEndian=sys::IsLittleEndianHost)
std::vector< Unit > CompileUnits
Definition: DWARFYAML.h:155
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE size_t size() const
size - Get the string size.
Definition: StringRef.h:138
void onStartCompileUnit(const DWARFYAML::Unit &CU) override
Const Visitor Functions.
InitialLength Length
Definition: DWARFYAML.h:102
std::vector< File > Files
Definition: DWARFYAML.h:140
std::vector< LineTable > DebugLines
Definition: DWARFYAML.h:157
void writeInitialLength(const DWARFYAML::InitialLength &Length, raw_ostream &OS, bool IsLittleEndian)
Error errorCodeToError(std::error_code EC)
Helper for converting an std::error_code to a Error.
Definition: Error.cpp:78
void onValue(const int64_t S, const bool LEB=false) override
A structured debug information entry.
Definition: DIE.h:662
void onValue(const uint16_t U) override
std::vector< StringRef > IncludeDirs
Definition: DWARFYAML.h:139
static void writeVariableSizedInteger(uint64_t Integer, size_t Size, raw_ostream &OS, bool IsLittleEndian)
raw_ostream & write(unsigned char C)
size_t getBufferSize() const
Definition: MemoryBuffer.h:175
void EmitPubSection(raw_ostream &OS, const PubSection &Sect, bool IsLittleEndian)
static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File)
void onValue(const uint32_t U) override
void(*)(raw_ostream &, const DWARFYAML::Data &) EmitFuncType
std::vector< StringRef > DebugStrings
Definition: DWARFYAML.h:147
llvm::yaml::Hex32 AbbrCode
Definition: DWARFYAML.h:97
void onValue(const StringRef String) override
static void EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc, StringRef Sec, StringMap< std::unique_ptr< MemoryBuffer >> &OutputBuffers)
StringMap - This is an unconventional map that is specialized for handling keys that are "strings"...
Definition: StringMap.h:220
static std::unique_ptr< MemoryBuffer > getMemBufferCopy(StringRef InputData, const Twine &BufferName="")
Open the specified memory range as a MemoryBuffer, copying the contents and taking ownership of it...
constexpr char Size[]
Key for Kernel::Arg::Metadata::mSize.
This file declares classes for handling the YAML representation of DWARF Debug Info.
std::vector< LineTableOpcode > Opcodes
Definition: DWARFYAML.h:141
Common declarations for yaml2obj.
void EmitDebugStr(raw_ostream &OS, const Data &DI)
void encodeSLEB128(int64_t Value, raw_ostream &OS, unsigned Padding=0)
Utility function to encode a SLEB128 value to an output stream.
Definition: LEB128.h:23
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Definition: raw_ostream.h:463
void onValue(const uint8_t U) override
Value visitors.
void onStartDIE(const DWARFYAML::Unit &CU, const DWARFYAML::Entry &DIE) override
This class implements an extremely fast bulk output stream that can only output to a stream...
Definition: raw_ostream.h:44
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:128
void encodeULEB128(uint64_t Value, raw_ostream &OS, unsigned Padding=0)
Utility function to encode a ULEB128 value to an output stream.
Definition: LEB128.h:73
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
void onValue(const uint64_t U, const bool LEB=false) override
void EmitDebugLine(raw_ostream &OS, const Data &DI)