Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 1 | //===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===// |
Chris Bieneman | 581f04c | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 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 |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 11 | /// The DWARF component of yaml2obj. Provided as library code for tests. |
Chris Bieneman | 581f04c | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 12 | /// |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 15 | #include "llvm/ObjectYAML/DWARFEmitter.h" |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 16 | #include "DWARFVisitor.h" |
| 17 | #include "llvm/ADT/StringMap.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
Chris Bieneman | 581f04c | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 19 | #include "llvm/ObjectYAML/DWARFYAML.h" |
| 20 | #include "llvm/Support/Error.h" |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 21 | #include "llvm/Support/Host.h" |
Chris Bieneman | 581f04c | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 22 | #include "llvm/Support/LEB128.h" |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 23 | #include "llvm/Support/MathExtras.h" |
| 24 | #include "llvm/Support/MemoryBuffer.h" |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 25 | #include "llvm/Support/SwapByteOrder.h" |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 26 | #include "llvm/Support/YAMLTraits.h" |
Chandler Carruth | e3e43d9 | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 27 | #include "llvm/Support/raw_ostream.h" |
Chris Bieneman | 7d46645 | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 28 | #include <algorithm> |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 29 | #include <cassert> |
| 30 | #include <cstddef> |
| 31 | #include <cstdint> |
| 32 | #include <memory> |
| 33 | #include <string> |
| 34 | #include <vector> |
Chris Bieneman | 7d46645 | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 35 | |
Chris Bieneman | 581f04c | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 36 | using namespace llvm; |
| 37 | |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 38 | template <typename T> |
Benjamin Kramer | 3264d3f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 39 | static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) { |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 40 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 41 | sys::swapByteOrder(Integer); |
| 42 | OS.write(reinterpret_cast<char *>(&Integer), sizeof(T)); |
| 43 | } |
| 44 | |
Benjamin Kramer | 3264d3f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 45 | static void writeVariableSizedInteger(uint64_t Integer, size_t Size, |
| 46 | raw_ostream &OS, bool IsLittleEndian) { |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 3264d3f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 59 | static void ZeroFillBytes(raw_ostream &OS, size_t Size) { |
Chris Bieneman | 18245f6 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 60 | std::vector<uint8_t> FillData; |
| 61 | FillData.insert(FillData.begin(), Size, 0); |
| 62 | OS.write(reinterpret_cast<char *>(FillData.data()), Size); |
| 63 | } |
| 64 | |
Benjamin Kramer | c773276 | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 65 | static void writeInitialLength(const DWARFYAML::InitialLength &Length, |
| 66 | raw_ostream &OS, bool IsLittleEndian) { |
Chris Bieneman | ad76c97 | 2017-03-03 21:11:55 +0000 | [diff] [blame] | 67 | writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian); |
| 68 | if (Length.isDWARF64()) |
| 69 | writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian); |
| 70 | } |
| 71 | |
Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 72 | void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) { |
Chris Bieneman | 581f04c | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 73 | for (auto Str : DI.DebugStrings) { |
| 74 | OS.write(Str.data(), Str.size()); |
| 75 | OS.write('\0'); |
| 76 | } |
| 77 | } |
| 78 | |
Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 79 | void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) { |
Chris Bieneman | 581f04c | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 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); |
Chris Bieneman | 64cf2ce | 2017-03-06 23:22:49 +0000 | [diff] [blame] | 87 | if (Attr.Form == dwarf::DW_FORM_implicit_const) |
| 88 | encodeSLEB128(Attr.Value, OS); |
Chris Bieneman | 581f04c | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 89 | } |
| 90 | encodeULEB128(0, OS); |
| 91 | encodeULEB128(0, OS); |
| 92 | } |
| 93 | } |
Chris Bieneman | 18245f6 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 94 | |
Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 95 | void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) { |
Chris Bieneman | 18245f6 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 96 | for (auto Range : DI.ARanges) { |
| 97 | auto HeaderStart = OS.tell(); |
Chris Bieneman | ad76c97 | 2017-03-03 21:11:55 +0000 | [diff] [blame] | 98 | writeInitialLength(Range.Length, OS, DI.IsLittleEndian); |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 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); |
Chris Bieneman | 18245f6 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 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) { |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 109 | writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS, |
| 110 | DI.IsLittleEndian); |
| 111 | writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS, |
| 112 | DI.IsLittleEndian); |
Chris Bieneman | 18245f6 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 113 | } |
| 114 | ZeroFillBytes(OS, Range.AddrSize * 2); |
| 115 | } |
| 116 | } |
Chris Bieneman | c2813d9 | 2016-12-19 22:22:12 +0000 | [diff] [blame] | 117 | |
Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 118 | void DWARFYAML::EmitPubSection(raw_ostream &OS, |
| 119 | const DWARFYAML::PubSection &Sect, |
| 120 | bool IsLittleEndian) { |
Chris Bieneman | ad76c97 | 2017-03-03 21:11:55 +0000 | [diff] [blame] | 121 | writeInitialLength(Sect.Length, OS, IsLittleEndian); |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 122 | writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian); |
| 123 | writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian); |
| 124 | writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian); |
Chris Bieneman | c2813d9 | 2016-12-19 22:22:12 +0000 | [diff] [blame] | 125 | for (auto Entry : Sect.Entries) { |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 126 | writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian); |
Chris Bieneman | c2813d9 | 2016-12-19 22:22:12 +0000 | [diff] [blame] | 127 | if (Sect.IsGNUStyle) |
Chris Bieneman | 5e76a3f | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 128 | writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian); |
Chris Bieneman | c2813d9 | 2016-12-19 22:22:12 +0000 | [diff] [blame] | 129 | OS.write(Entry.Name.data(), Entry.Name.size()); |
| 130 | OS.write('\0'); |
| 131 | } |
Chris Bieneman | 7d46645 | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Benjamin Kramer | c773276 | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 134 | namespace { |
Adrian Prantl | 26b584c | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 135 | /// An extension of the DWARFYAML::ConstVisitor which writes compile |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 136 | /// units and DIEs to a stream. |
| 137 | class DumpVisitor : public DWARFYAML::ConstVisitor { |
| 138 | raw_ostream &OS; |
Chris Bieneman | 7d46645 | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 139 | |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 140 | protected: |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 141 | void onStartCompileUnit(const DWARFYAML::Unit &CU) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 142 | writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian); |
| 143 | writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian); |
Chris Bieneman | da6d07a | 2017-03-07 18:50:58 +0000 | [diff] [blame] | 144 | if(CU.Version >= 5) { |
| 145 | writeInteger((uint8_t)CU.Type, OS, DebugInfo.IsLittleEndian); |
| 146 | writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian); |
| 147 | writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian); |
| 148 | }else { |
| 149 | writeInteger((uint32_t)CU.AbbrOffset, OS, DebugInfo.IsLittleEndian); |
| 150 | writeInteger((uint8_t)CU.AddrSize, OS, DebugInfo.IsLittleEndian); |
| 151 | } |
Chris Bieneman | 7d46645 | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 152 | } |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 153 | |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 154 | void onStartDIE(const DWARFYAML::Unit &CU, |
| 155 | const DWARFYAML::Entry &DIE) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 156 | encodeULEB128(DIE.AbbrCode, OS); |
| 157 | } |
| 158 | |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 159 | void onValue(const uint8_t U) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 160 | writeInteger(U, OS, DebugInfo.IsLittleEndian); |
| 161 | } |
| 162 | |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 163 | void onValue(const uint16_t U) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 164 | writeInteger(U, OS, DebugInfo.IsLittleEndian); |
| 165 | } |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 166 | |
| 167 | void onValue(const uint32_t U) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 168 | writeInteger(U, OS, DebugInfo.IsLittleEndian); |
| 169 | } |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 170 | |
| 171 | void onValue(const uint64_t U, const bool LEB = false) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 172 | if (LEB) |
| 173 | encodeULEB128(U, OS); |
| 174 | else |
| 175 | writeInteger(U, OS, DebugInfo.IsLittleEndian); |
| 176 | } |
| 177 | |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 178 | void onValue(const int64_t S, const bool LEB = false) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 179 | if (LEB) |
| 180 | encodeSLEB128(S, OS); |
| 181 | else |
| 182 | writeInteger(S, OS, DebugInfo.IsLittleEndian); |
| 183 | } |
| 184 | |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 185 | void onValue(const StringRef String) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 186 | OS.write(String.data(), String.size()); |
| 187 | OS.write('\0'); |
| 188 | } |
| 189 | |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 190 | void onValue(const MemoryBufferRef MBR) override { |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 191 | OS.write(MBR.getBufferStart(), MBR.getBufferSize()); |
| 192 | } |
| 193 | |
| 194 | public: |
| 195 | DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out) |
| 196 | : DWARFYAML::ConstVisitor(DI), OS(Out) {} |
| 197 | }; |
Benjamin Kramer | c773276 | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 198 | } // namespace |
Chris Bieneman | 5cd1dc4 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 199 | |
| 200 | void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) { |
| 201 | DumpVisitor Visitor(DI, OS); |
| 202 | Visitor.traverseDebugInfo(); |
Chris Bieneman | 7d46645 | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 203 | } |
Chris Bieneman | 6c9c3a7 | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 204 | |
Benjamin Kramer | 3264d3f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 205 | static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) { |
Chris Bieneman | 6c9c3a7 | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 206 | OS.write(File.Name.data(), File.Name.size()); |
| 207 | OS.write('\0'); |
| 208 | encodeULEB128(File.DirIdx, OS); |
| 209 | encodeULEB128(File.ModTime, OS); |
| 210 | encodeULEB128(File.Length, OS); |
| 211 | } |
| 212 | |
Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 213 | void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) { |
Benjamin Kramer | 3264d3f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 214 | for (const auto &LineTable : DI.DebugLines) { |
Chris Bieneman | ad76c97 | 2017-03-03 21:11:55 +0000 | [diff] [blame] | 215 | writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian); |
| 216 | uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4; |
Chris Bieneman | 6c9c3a7 | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 217 | writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian); |
| 218 | writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength, |
| 219 | OS, DI.IsLittleEndian); |
| 220 | writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian); |
| 221 | if (LineTable.Version >= 4) |
| 222 | writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian); |
| 223 | writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian); |
| 224 | writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian); |
| 225 | writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian); |
| 226 | writeInteger((uint8_t)LineTable.OpcodeBase, OS, DI.IsLittleEndian); |
| 227 | |
| 228 | for (auto OpcodeLength : LineTable.StandardOpcodeLengths) |
| 229 | writeInteger((uint8_t)OpcodeLength, OS, DI.IsLittleEndian); |
| 230 | |
| 231 | for (auto IncludeDir : LineTable.IncludeDirs) { |
| 232 | OS.write(IncludeDir.data(), IncludeDir.size()); |
| 233 | OS.write('\0'); |
| 234 | } |
| 235 | OS.write('\0'); |
| 236 | |
| 237 | for (auto File : LineTable.Files) |
Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 238 | EmitFileEntry(OS, File); |
Chris Bieneman | 6c9c3a7 | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 239 | OS.write('\0'); |
| 240 | |
| 241 | for (auto Op : LineTable.Opcodes) { |
| 242 | writeInteger((uint8_t)Op.Opcode, OS, DI.IsLittleEndian); |
| 243 | if (Op.Opcode == 0) { |
| 244 | encodeULEB128(Op.ExtLen, OS); |
| 245 | writeInteger((uint8_t)Op.SubOpcode, OS, DI.IsLittleEndian); |
| 246 | switch (Op.SubOpcode) { |
| 247 | case dwarf::DW_LNE_set_address: |
| 248 | case dwarf::DW_LNE_set_discriminator: |
| 249 | writeVariableSizedInteger(Op.Data, DI.CompileUnits[0].AddrSize, OS, |
| 250 | DI.IsLittleEndian); |
| 251 | break; |
| 252 | case dwarf::DW_LNE_define_file: |
Chris Bieneman | 15135b3 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 253 | EmitFileEntry(OS, Op.FileEntry); |
Chris Bieneman | 6c9c3a7 | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 254 | break; |
| 255 | case dwarf::DW_LNE_end_sequence: |
| 256 | break; |
| 257 | default: |
| 258 | for (auto OpByte : Op.UnknownOpcodeData) |
| 259 | writeInteger((uint8_t)OpByte, OS, DI.IsLittleEndian); |
| 260 | } |
| 261 | } else if (Op.Opcode < LineTable.OpcodeBase) { |
| 262 | switch (Op.Opcode) { |
| 263 | case dwarf::DW_LNS_copy: |
| 264 | case dwarf::DW_LNS_negate_stmt: |
| 265 | case dwarf::DW_LNS_set_basic_block: |
| 266 | case dwarf::DW_LNS_const_add_pc: |
| 267 | case dwarf::DW_LNS_set_prologue_end: |
| 268 | case dwarf::DW_LNS_set_epilogue_begin: |
| 269 | break; |
| 270 | |
| 271 | case dwarf::DW_LNS_advance_pc: |
| 272 | case dwarf::DW_LNS_set_file: |
| 273 | case dwarf::DW_LNS_set_column: |
| 274 | case dwarf::DW_LNS_set_isa: |
| 275 | encodeULEB128(Op.Data, OS); |
| 276 | break; |
| 277 | |
| 278 | case dwarf::DW_LNS_advance_line: |
| 279 | encodeSLEB128(Op.SData, OS); |
| 280 | break; |
| 281 | |
| 282 | case dwarf::DW_LNS_fixed_advance_pc: |
| 283 | writeInteger((uint16_t)Op.Data, OS, DI.IsLittleEndian); |
| 284 | break; |
| 285 | |
| 286 | default: |
| 287 | for (auto OpData : Op.StandardOpcodeData) { |
| 288 | encodeULEB128(OpData, OS); |
| 289 | } |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | } |
| 294 | } |
Chris Bieneman | 70ebae9 | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 295 | |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 296 | using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &); |
Chris Bieneman | 70ebae9 | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 297 | |
Benjamin Kramer | 3264d3f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 298 | static void |
| 299 | EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc, |
| 300 | StringRef Sec, |
| 301 | StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) { |
Chris Bieneman | 70ebae9 | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 302 | std::string Data; |
| 303 | raw_string_ostream DebugInfoStream(Data); |
| 304 | EmitFunc(DebugInfoStream, DI); |
| 305 | DebugInfoStream.flush(); |
| 306 | if (!Data.empty()) |
| 307 | OutputBuffers[Sec] = MemoryBuffer::getMemBufferCopy(Data); |
| 308 | } |
| 309 | |
Benjamin Kramer | fd83828 | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 310 | namespace { |
Jonas Devlieghere | f6415de | 2018-04-20 12:33:49 +0000 | [diff] [blame] | 311 | class DIEFixupVisitor : public DWARFYAML::Visitor { |
| 312 | uint64_t Length; |
Chris Bieneman | 70ebae9 | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 313 | |
Jonas Devlieghere | f6415de | 2018-04-20 12:33:49 +0000 | [diff] [blame] | 314 | public: |
| 315 | DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){}; |
| 316 | |
| 317 | private: |
| 318 | virtual void onStartCompileUnit(DWARFYAML::Unit &CU) { Length = 7; } |
| 319 | |
| 320 | virtual void onEndCompileUnit(DWARFYAML::Unit &CU) { |
| 321 | CU.Length.setLength(Length); |
| 322 | } |
| 323 | |
| 324 | virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) { |
| 325 | Length += getULEB128Size(DIE.AbbrCode); |
| 326 | } |
| 327 | |
| 328 | virtual void onValue(const uint8_t U) { Length += 1; } |
| 329 | virtual void onValue(const uint16_t U) { Length += 2; } |
| 330 | virtual void onValue(const uint32_t U) { Length += 4; } |
| 331 | virtual void onValue(const uint64_t U, const bool LEB = false) { |
| 332 | if (LEB) |
| 333 | Length += getULEB128Size(U); |
| 334 | else |
| 335 | Length += 8; |
| 336 | } |
| 337 | virtual void onValue(const int64_t S, const bool LEB = false) { |
| 338 | if (LEB) |
| 339 | Length += getSLEB128Size(S); |
| 340 | else |
| 341 | Length += 8; |
| 342 | } |
| 343 | virtual void onValue(const StringRef String) { Length += String.size() + 1; } |
| 344 | |
| 345 | virtual void onValue(const MemoryBufferRef MBR) { |
| 346 | Length += MBR.getBufferSize(); |
| 347 | } |
| 348 | }; |
Benjamin Kramer | fd83828 | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 349 | } // namespace |
Jonas Devlieghere | f6415de | 2018-04-20 12:33:49 +0000 | [diff] [blame] | 350 | |
| 351 | Expected<StringMap<std::unique_ptr<MemoryBuffer>>> |
| 352 | DWARFYAML::EmitDebugSections(StringRef YAMLString, bool ApplyFixups, |
| 353 | bool IsLittleEndian) { |
Chris Bieneman | 70ebae9 | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 354 | yaml::Input YIn(YAMLString); |
| 355 | |
| 356 | DWARFYAML::Data DI; |
| 357 | DI.IsLittleEndian = IsLittleEndian; |
| 358 | YIn >> DI; |
| 359 | if (YIn.error()) |
| 360 | return errorCodeToError(YIn.error()); |
| 361 | |
Jonas Devlieghere | f6415de | 2018-04-20 12:33:49 +0000 | [diff] [blame] | 362 | if (ApplyFixups) { |
| 363 | DIEFixupVisitor DIFixer(DI); |
| 364 | DIFixer.traverseDebugInfo(); |
| 365 | } |
| 366 | |
| 367 | StringMap<std::unique_ptr<MemoryBuffer>> DebugSections; |
Chris Bieneman | 70ebae9 | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 368 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info", |
| 369 | DebugSections); |
| 370 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line", |
| 371 | DebugSections); |
| 372 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str", |
| 373 | DebugSections); |
| 374 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev", |
| 375 | DebugSections); |
| 376 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges", |
| 377 | DebugSections); |
| 378 | return std::move(DebugSections); |
| 379 | } |