annotate lib/ObjectYAML/DWARFEmitter.cpp @ 124:4fa72497ed5d

fix
author mir3636
date Thu, 30 Nov 2017 20:04:56 +0900
parents 803732b1fca8
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 /// \file
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 /// \brief The DWARF component of yaml2obj. Provided as library code for tests.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #include "llvm/ObjectYAML/DWARFEmitter.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16 #include "DWARFVisitor.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include "llvm/ADT/StringMap.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include "llvm/ADT/StringRef.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include "llvm/ObjectYAML/DWARFYAML.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include "llvm/Support/Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21 #include "llvm/Support/Host.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 #include "llvm/Support/LEB128.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 #include "llvm/Support/MathExtras.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 #include "llvm/Support/MemoryBuffer.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 #include "llvm/Support/SwapByteOrder.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26 #include "llvm/Support/YAMLTraits.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 #include "llvm/Support/raw_ostream.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 #include <algorithm>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29 #include <cassert>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 #include <cstddef>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 #include <cstdint>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 #include <memory>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 #include <string>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 #include <vector>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 using namespace llvm;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 template <typename T>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 if (IsLittleEndian != sys::IsLittleEndianHost)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 sys::swapByteOrder(Integer);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 OS.write(reinterpret_cast<char *>(&Integer), sizeof(T));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 static void writeVariableSizedInteger(uint64_t Integer, size_t Size,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 raw_ostream &OS, bool IsLittleEndian) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 if (8 == Size)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 writeInteger((uint64_t)Integer, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 else if (4 == Size)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 writeInteger((uint32_t)Integer, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 else if (2 == Size)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 writeInteger((uint16_t)Integer, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 else if (1 == Size)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 writeInteger((uint8_t)Integer, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 assert(false && "Invalid integer write size.");
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 static void ZeroFillBytes(raw_ostream &OS, size_t Size) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 std::vector<uint8_t> FillData;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 FillData.insert(FillData.begin(), Size, 0);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 OS.write(reinterpret_cast<char *>(FillData.data()), Size);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 static void writeInitialLength(const DWARFYAML::InitialLength &Length,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 raw_ostream &OS, bool IsLittleEndian) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 if (Length.isDWARF64())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 for (auto Str : DI.DebugStrings) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 OS.write(Str.data(), Str.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 OS.write('\0');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80 for (auto AbbrevDecl : DI.AbbrevDecls) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 encodeULEB128(AbbrevDecl.Code, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 encodeULEB128(AbbrevDecl.Tag, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 OS.write(AbbrevDecl.Children);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 for (auto Attr : AbbrevDecl.Attributes) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85 encodeULEB128(Attr.Attribute, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 encodeULEB128(Attr.Form, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 if (Attr.Form == dwarf::DW_FORM_implicit_const)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 encodeSLEB128(Attr.Value, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 encodeULEB128(0, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91 encodeULEB128(0, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95 void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 for (auto Range : DI.ARanges) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 auto HeaderStart = OS.tell();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 writeInitialLength(Range.Length, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99 writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 auto HeaderSize = OS.tell() - HeaderStart;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105 auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 ZeroFillBytes(OS, FirstDescriptor - HeaderSize);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 for (auto Descriptor : Range.Descriptors) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109 writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114 ZeroFillBytes(OS, Range.AddrSize * 2);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 void DWARFYAML::EmitPubSection(raw_ostream &OS,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 const DWARFYAML::PubSection &Sect,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120 bool IsLittleEndian) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 writeInitialLength(Sect.Length, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123 writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 for (auto Entry : Sect.Entries) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 if (Sect.IsGNUStyle)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 OS.write(Entry.Name.data(), Entry.Name.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 OS.write('\0');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 namespace {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 /// \brief An extension of the DWARFYAML::ConstVisitor which writes compile
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 /// units and DIEs to a stream.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 class DumpVisitor : public DWARFYAML::ConstVisitor {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138 raw_ostream &OS;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 protected:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 void onStartCompileUnit(const DWARFYAML::Unit &CU) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142 writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 if(CU.Version >= 5) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145 writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 }else {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 void onStartDIE(const DWARFYAML::Unit &CU,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 const DWARFYAML::Entry &DIE) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 encodeULEB128(DIE.AbbrCode, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 void onValue(const uint8_t U) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 writeInteger(U, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 void onValue(const uint16_t U) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 writeInteger(U, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 void onValue(const uint32_t U) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 writeInteger(U, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 void onValue(const uint64_t U, const bool LEB = false) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 if (LEB)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 encodeULEB128(U, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 writeInteger(U, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 void onValue(const int64_t S, const bool LEB = false) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 if (LEB)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 encodeSLEB128(S, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 writeInteger(S, OS, DebugInfo.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 void onValue(const StringRef String) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 OS.write(String.data(), String.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188 OS.write('\0');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 void onValue(const MemoryBufferRef MBR) override {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 OS.write(MBR.getBufferStart(), MBR.getBufferSize());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 : DWARFYAML::ConstVisitor(DI), OS(Out) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 } // namespace
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 DumpVisitor Visitor(DI, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 Visitor.traverseDebugInfo();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206 static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 OS.write(File.Name.data(), File.Name.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 OS.write('\0');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209 encodeULEB128(File.DirIdx, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 encodeULEB128(File.ModTime, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211 encodeULEB128(File.Length, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214 void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 for (const auto &LineTable : DI.DebugLines) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218 writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219 writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222 if (LineTable.Version >= 4)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226 writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227 writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 for (auto OpcodeLength : LineTable.StandardOpcodeLengths)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 for (auto IncludeDir : LineTable.IncludeDirs) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 OS.write(IncludeDir.data(), IncludeDir.size());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234 OS.write('\0');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 OS.write('\0');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238 for (auto File : LineTable.Files)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239 EmitFileEntry(OS, File);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 OS.write('\0');
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 for (auto Op : LineTable.Opcodes) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243 writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 if (Op.Opcode == 0) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245 encodeULEB128(Op.ExtLen, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247 switch (Op.SubOpcode) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 case dwarf::DW_LNE_set_address:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 case dwarf::DW_LNE_set_discriminator:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250 writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251 DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253 case dwarf::DW_LNE_define_file:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254 EmitFileEntry(OS, Op.FileEntry);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256 case dwarf::DW_LNE_end_sequence:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259 for (auto OpByte : Op.UnknownOpcodeData)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 } else if (Op.Opcode < LineTable.OpcodeBase) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263 switch (Op.Opcode) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264 case dwarf::DW_LNS_copy:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 case dwarf::DW_LNS_negate_stmt:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266 case dwarf::DW_LNS_set_basic_block:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 case dwarf::DW_LNS_const_add_pc:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268 case dwarf::DW_LNS_set_prologue_end:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269 case dwarf::DW_LNS_set_epilogue_begin:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 case dwarf::DW_LNS_advance_pc:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273 case dwarf::DW_LNS_set_file:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 case dwarf::DW_LNS_set_column:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275 case dwarf::DW_LNS_set_isa:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 encodeULEB128(Op.Data, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
278
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279 case dwarf::DW_LNS_advance_line:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280 encodeSLEB128(Op.SData, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
282
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283 case dwarf::DW_LNS_fixed_advance_pc:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 break;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287 default:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 for (auto OpData : Op.StandardOpcodeData) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289 encodeULEB128(OpData, OS);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 static void
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300 EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301 StringRef Sec,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302 StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303 std::string Data;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304 raw_string_ostream DebugInfoStream(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305 EmitFunc(DebugInfoStream, DI);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 DebugInfoStream.flush();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307 if (!Data.empty())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308 OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311 Expected<StringMap<std::unique_ptr<MemoryBuffer>>>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312 DWARFYAML::EmitDebugSections(StringRef YAMLString,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 bool IsLittleEndian) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 StringMap<std::unique_ptr<MemoryBuffer>> DebugSections;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316 yaml::Input YIn(YAMLString);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 DWARFYAML::Data DI;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 DI.IsLittleEndian = IsLittleEndian;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320 YIn >> DI;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321 if (YIn.error())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 return errorCodeToError(YIn.error());
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 DebugSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 DebugSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329 DebugSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331 DebugSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332 EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 DebugSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 return std::move(DebugSections);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 }