Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 1 | //===- DWARFEmitter - Convert YAML to DWARF binary data -------------------===// |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | /// |
| 9 | /// \file |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 10 | /// The DWARF component of yaml2obj. Provided as library code for tests. |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 11 | /// |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 14 | #include "llvm/ObjectYAML/DWARFEmitter.h" |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 15 | #include "DWARFVisitor.h" |
| 16 | #include "llvm/ADT/StringMap.h" |
| 17 | #include "llvm/ADT/StringRef.h" |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 18 | #include "llvm/ObjectYAML/DWARFYAML.h" |
| 19 | #include "llvm/Support/Error.h" |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 20 | #include "llvm/Support/Host.h" |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 21 | #include "llvm/Support/LEB128.h" |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 22 | #include "llvm/Support/MathExtras.h" |
| 23 | #include "llvm/Support/MemoryBuffer.h" |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 24 | #include "llvm/Support/SwapByteOrder.h" |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 25 | #include "llvm/Support/YAMLTraits.h" |
Chandler Carruth | 6bda14b | 2017-06-06 11:49:48 +0000 | [diff] [blame] | 26 | #include "llvm/Support/raw_ostream.h" |
Chris Bieneman | e0e451d | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 27 | #include <algorithm> |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 28 | #include <cassert> |
| 29 | #include <cstddef> |
| 30 | #include <cstdint> |
| 31 | #include <memory> |
| 32 | #include <string> |
| 33 | #include <vector> |
Chris Bieneman | e0e451d | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 34 | |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 35 | using namespace llvm; |
| 36 | |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 37 | template <typename T> |
Benjamin Kramer | efcf06f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 38 | static void writeInteger(T Integer, raw_ostream &OS, bool IsLittleEndian) { |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 39 | if (IsLittleEndian != sys::IsLittleEndianHost) |
| 40 | sys::swapByteOrder(Integer); |
| 41 | OS.write(reinterpret_cast<char *>(&Integer), sizeof(T)); |
| 42 | } |
| 43 | |
Benjamin Kramer | efcf06f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 44 | static void writeVariableSizedInteger(uint64_t Integer, size_t Size, |
| 45 | raw_ostream &OS, bool IsLittleEndian) { |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 46 | if (8 == Size) |
| 47 | writeInteger((uint64_t)Integer, OS, IsLittleEndian); |
| 48 | else if (4 == Size) |
| 49 | writeInteger((uint32_t)Integer, OS, IsLittleEndian); |
| 50 | else if (2 == Size) |
| 51 | writeInteger((uint16_t)Integer, OS, IsLittleEndian); |
| 52 | else if (1 == Size) |
| 53 | writeInteger((uint8_t)Integer, OS, IsLittleEndian); |
| 54 | else |
| 55 | assert(false && "Invalid integer write size."); |
| 56 | } |
| 57 | |
Benjamin Kramer | efcf06f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 58 | static void ZeroFillBytes(raw_ostream &OS, size_t Size) { |
Chris Bieneman | 313b326 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 59 | std::vector<uint8_t> FillData; |
| 60 | FillData.insert(FillData.begin(), Size, 0); |
| 61 | OS.write(reinterpret_cast<char *>(FillData.data()), Size); |
| 62 | } |
| 63 | |
Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 64 | static void writeInitialLength(const DWARFYAML::InitialLength &Length, |
| 65 | raw_ostream &OS, bool IsLittleEndian) { |
Chris Bieneman | faf1feb | 2017-03-03 21:11:55 +0000 | [diff] [blame] | 66 | writeInteger((uint32_t)Length.TotalLength, OS, IsLittleEndian); |
| 67 | if (Length.isDWARF64()) |
| 68 | writeInteger((uint64_t)Length.TotalLength64, OS, IsLittleEndian); |
| 69 | } |
| 70 | |
Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 71 | void DWARFYAML::EmitDebugStr(raw_ostream &OS, const DWARFYAML::Data &DI) { |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 72 | for (auto Str : DI.DebugStrings) { |
| 73 | OS.write(Str.data(), Str.size()); |
| 74 | OS.write('\0'); |
| 75 | } |
| 76 | } |
| 77 | |
Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 78 | void DWARFYAML::EmitDebugAbbrev(raw_ostream &OS, const DWARFYAML::Data &DI) { |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 79 | for (auto AbbrevDecl : DI.AbbrevDecls) { |
| 80 | encodeULEB128(AbbrevDecl.Code, OS); |
| 81 | encodeULEB128(AbbrevDecl.Tag, OS); |
| 82 | OS.write(AbbrevDecl.Children); |
| 83 | for (auto Attr : AbbrevDecl.Attributes) { |
| 84 | encodeULEB128(Attr.Attribute, OS); |
| 85 | encodeULEB128(Attr.Form, OS); |
Chris Bieneman | bcf513f | 2017-03-06 23:22:49 +0000 | [diff] [blame] | 86 | if (Attr.Form == dwarf::DW_FORM_implicit_const) |
| 87 | encodeSLEB128(Attr.Value, OS); |
Chris Bieneman | 7d7364a | 2016-12-07 22:30:15 +0000 | [diff] [blame] | 88 | } |
| 89 | encodeULEB128(0, OS); |
| 90 | encodeULEB128(0, OS); |
| 91 | } |
| 92 | } |
Chris Bieneman | 313b326 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 93 | |
Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 94 | void DWARFYAML::EmitDebugAranges(raw_ostream &OS, const DWARFYAML::Data &DI) { |
Chris Bieneman | 313b326 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 95 | for (auto Range : DI.ARanges) { |
| 96 | auto HeaderStart = OS.tell(); |
Chris Bieneman | faf1feb | 2017-03-03 21:11:55 +0000 | [diff] [blame] | 97 | writeInitialLength(Range.Length, OS, DI.IsLittleEndian); |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 98 | writeInteger((uint16_t)Range.Version, OS, DI.IsLittleEndian); |
| 99 | writeInteger((uint32_t)Range.CuOffset, OS, DI.IsLittleEndian); |
| 100 | writeInteger((uint8_t)Range.AddrSize, OS, DI.IsLittleEndian); |
| 101 | writeInteger((uint8_t)Range.SegSize, OS, DI.IsLittleEndian); |
Chris Bieneman | 313b326 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 102 | |
| 103 | auto HeaderSize = OS.tell() - HeaderStart; |
| 104 | auto FirstDescriptor = alignTo(HeaderSize, Range.AddrSize * 2); |
| 105 | ZeroFillBytes(OS, FirstDescriptor - HeaderSize); |
| 106 | |
| 107 | for (auto Descriptor : Range.Descriptors) { |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 108 | writeVariableSizedInteger(Descriptor.Address, Range.AddrSize, OS, |
| 109 | DI.IsLittleEndian); |
| 110 | writeVariableSizedInteger(Descriptor.Length, Range.AddrSize, OS, |
| 111 | DI.IsLittleEndian); |
Chris Bieneman | 313b326 | 2016-12-09 00:26:44 +0000 | [diff] [blame] | 112 | } |
| 113 | ZeroFillBytes(OS, Range.AddrSize * 2); |
| 114 | } |
| 115 | } |
Chris Bieneman | d943094 | 2016-12-19 22:22:12 +0000 | [diff] [blame] | 116 | |
Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 117 | void DWARFYAML::EmitPubSection(raw_ostream &OS, |
| 118 | const DWARFYAML::PubSection &Sect, |
| 119 | bool IsLittleEndian) { |
Chris Bieneman | faf1feb | 2017-03-03 21:11:55 +0000 | [diff] [blame] | 120 | writeInitialLength(Sect.Length, OS, IsLittleEndian); |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 121 | writeInteger((uint16_t)Sect.Version, OS, IsLittleEndian); |
| 122 | writeInteger((uint32_t)Sect.UnitOffset, OS, IsLittleEndian); |
| 123 | writeInteger((uint32_t)Sect.UnitSize, OS, IsLittleEndian); |
Chris Bieneman | d943094 | 2016-12-19 22:22:12 +0000 | [diff] [blame] | 124 | for (auto Entry : Sect.Entries) { |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 125 | writeInteger((uint32_t)Entry.DieOffset, OS, IsLittleEndian); |
Chris Bieneman | d943094 | 2016-12-19 22:22:12 +0000 | [diff] [blame] | 126 | if (Sect.IsGNUStyle) |
Chris Bieneman | 55de3a2 | 2016-12-22 21:58:03 +0000 | [diff] [blame] | 127 | writeInteger((uint32_t)Entry.Descriptor, OS, IsLittleEndian); |
Chris Bieneman | d943094 | 2016-12-19 22:22:12 +0000 | [diff] [blame] | 128 | OS.write(Entry.Name.data(), Entry.Name.size()); |
| 129 | OS.write('\0'); |
| 130 | } |
Chris Bieneman | e0e451d | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 131 | } |
| 132 | |
Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 133 | namespace { |
Adrian Prantl | 5f8f34e4 | 2018-05-01 15:54:18 +0000 | [diff] [blame] | 134 | /// An extension of the DWARFYAML::ConstVisitor which writes compile |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 135 | /// units and DIEs to a stream. |
| 136 | class DumpVisitor : public DWARFYAML::ConstVisitor { |
| 137 | raw_ostream &OS; |
Chris Bieneman | e0e451d | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 138 | |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 139 | protected: |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 140 | void onStartCompileUnit(const DWARFYAML::Unit &CU) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 141 | writeInitialLength(CU.Length, OS, DebugInfo.IsLittleEndian); |
| 142 | writeInteger((uint16_t)CU.Version, OS, DebugInfo.IsLittleEndian); |
Chris Bieneman | b3ca711 | 2017-03-07 18:50:58 +0000 | [diff] [blame] | 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 | } |
Chris Bieneman | e0e451d | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 151 | } |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 152 | |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 153 | void onStartDIE(const DWARFYAML::Unit &CU, |
| 154 | const DWARFYAML::Entry &DIE) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 155 | encodeULEB128(DIE.AbbrCode, OS); |
| 156 | } |
| 157 | |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 158 | void onValue(const uint8_t U) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 159 | writeInteger(U, OS, DebugInfo.IsLittleEndian); |
| 160 | } |
| 161 | |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 162 | void onValue(const uint16_t U) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 163 | writeInteger(U, OS, DebugInfo.IsLittleEndian); |
| 164 | } |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 165 | |
| 166 | void onValue(const uint32_t U) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 167 | writeInteger(U, OS, DebugInfo.IsLittleEndian); |
| 168 | } |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 169 | |
| 170 | void onValue(const uint64_t U, const bool LEB = false) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 171 | if (LEB) |
| 172 | encodeULEB128(U, OS); |
| 173 | else |
| 174 | writeInteger(U, OS, DebugInfo.IsLittleEndian); |
| 175 | } |
| 176 | |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 177 | void onValue(const int64_t S, const bool LEB = false) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 178 | if (LEB) |
| 179 | encodeSLEB128(S, OS); |
| 180 | else |
| 181 | writeInteger(S, OS, DebugInfo.IsLittleEndian); |
| 182 | } |
| 183 | |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 184 | void onValue(const StringRef String) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 185 | OS.write(String.data(), String.size()); |
| 186 | OS.write('\0'); |
| 187 | } |
| 188 | |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 189 | void onValue(const MemoryBufferRef MBR) override { |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 190 | OS.write(MBR.getBufferStart(), MBR.getBufferSize()); |
| 191 | } |
| 192 | |
| 193 | public: |
| 194 | DumpVisitor(const DWARFYAML::Data &DI, raw_ostream &Out) |
| 195 | : DWARFYAML::ConstVisitor(DI), OS(Out) {} |
| 196 | }; |
Benjamin Kramer | 49a49fe | 2017-08-20 13:03:48 +0000 | [diff] [blame] | 197 | } // namespace |
Chris Bieneman | 8f471f7 | 2017-03-06 20:52:12 +0000 | [diff] [blame] | 198 | |
| 199 | void DWARFYAML::EmitDebugInfo(raw_ostream &OS, const DWARFYAML::Data &DI) { |
| 200 | DumpVisitor Visitor(DI, OS); |
| 201 | Visitor.traverseDebugInfo(); |
Chris Bieneman | e0e451d | 2016-12-22 22:44:27 +0000 | [diff] [blame] | 202 | } |
Chris Bieneman | 1b7200d | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 203 | |
Benjamin Kramer | efcf06f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 204 | static void EmitFileEntry(raw_ostream &OS, const DWARFYAML::File &File) { |
Chris Bieneman | 1b7200d | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 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 | |
Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 212 | void DWARFYAML::EmitDebugLine(raw_ostream &OS, const DWARFYAML::Data &DI) { |
Benjamin Kramer | efcf06f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 213 | for (const auto &LineTable : DI.DebugLines) { |
Chris Bieneman | faf1feb | 2017-03-03 21:11:55 +0000 | [diff] [blame] | 214 | writeInitialLength(LineTable.Length, OS, DI.IsLittleEndian); |
| 215 | uint64_t SizeOfPrologueLength = LineTable.Length.isDWARF64() ? 8 : 4; |
Chris Bieneman | 1b7200d | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 216 | writeInteger((uint16_t)LineTable.Version, OS, DI.IsLittleEndian); |
| 217 | writeVariableSizedInteger(LineTable.PrologueLength, SizeOfPrologueLength, |
| 218 | OS, DI.IsLittleEndian); |
| 219 | writeInteger((uint8_t)LineTable.MinInstLength, OS, DI.IsLittleEndian); |
| 220 | if (LineTable.Version >= 4) |
| 221 | writeInteger((uint8_t)LineTable.MaxOpsPerInst, OS, DI.IsLittleEndian); |
| 222 | writeInteger((uint8_t)LineTable.DefaultIsStmt, OS, DI.IsLittleEndian); |
| 223 | writeInteger((uint8_t)LineTable.LineBase, OS, DI.IsLittleEndian); |
| 224 | writeInteger((uint8_t)LineTable.LineRange, OS, DI.IsLittleEndian); |
| 225 | writeInteger((uint8_t)LineTable.OpcodeBase, 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) |
Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 237 | EmitFileEntry(OS, File); |
Chris Bieneman | 1b7200d | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 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: |
Chris Bieneman | 07088c1 | 2017-01-12 21:35:21 +0000 | [diff] [blame] | 252 | EmitFileEntry(OS, Op.FileEntry); |
Chris Bieneman | 1b7200d | 2017-01-10 06:22:49 +0000 | [diff] [blame] | 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 | } |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 294 | |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 295 | using EmitFuncType = void (*)(raw_ostream &, const DWARFYAML::Data &); |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 296 | |
Benjamin Kramer | efcf06f | 2017-02-11 11:06:55 +0000 | [diff] [blame] | 297 | static void |
| 298 | EmitDebugSectionImpl(const DWARFYAML::Data &DI, EmitFuncType EmitFunc, |
| 299 | StringRef Sec, |
| 300 | StringMap<std::unique_ptr<MemoryBuffer>> &OutputBuffers) { |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 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 | |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 309 | namespace { |
Jonas Devlieghere | 5c709ed | 2018-04-20 12:33:49 +0000 | [diff] [blame] | 310 | class DIEFixupVisitor : public DWARFYAML::Visitor { |
| 311 | uint64_t Length; |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 312 | |
Jonas Devlieghere | 5c709ed | 2018-04-20 12:33:49 +0000 | [diff] [blame] | 313 | public: |
| 314 | DIEFixupVisitor(DWARFYAML::Data &DI) : DWARFYAML::Visitor(DI){}; |
| 315 | |
| 316 | private: |
| 317 | virtual void onStartCompileUnit(DWARFYAML::Unit &CU) { Length = 7; } |
| 318 | |
| 319 | virtual void onEndCompileUnit(DWARFYAML::Unit &CU) { |
| 320 | CU.Length.setLength(Length); |
| 321 | } |
| 322 | |
| 323 | virtual void onStartDIE(DWARFYAML::Unit &CU, DWARFYAML::Entry &DIE) { |
| 324 | Length += getULEB128Size(DIE.AbbrCode); |
| 325 | } |
| 326 | |
| 327 | virtual void onValue(const uint8_t U) { Length += 1; } |
| 328 | virtual void onValue(const uint16_t U) { Length += 2; } |
| 329 | virtual void onValue(const uint32_t U) { Length += 4; } |
| 330 | virtual void onValue(const uint64_t U, const bool LEB = false) { |
| 331 | if (LEB) |
| 332 | Length += getULEB128Size(U); |
| 333 | else |
| 334 | Length += 8; |
| 335 | } |
| 336 | virtual void onValue(const int64_t S, const bool LEB = false) { |
| 337 | if (LEB) |
| 338 | Length += getSLEB128Size(S); |
| 339 | else |
| 340 | Length += 8; |
| 341 | } |
| 342 | virtual void onValue(const StringRef String) { Length += String.size() + 1; } |
| 343 | |
| 344 | virtual void onValue(const MemoryBufferRef MBR) { |
| 345 | Length += MBR.getBufferSize(); |
| 346 | } |
| 347 | }; |
Benjamin Kramer | 651d0bf | 2018-05-15 21:26:47 +0000 | [diff] [blame] | 348 | } // namespace |
Jonas Devlieghere | 5c709ed | 2018-04-20 12:33:49 +0000 | [diff] [blame] | 349 | |
| 350 | Expected<StringMap<std::unique_ptr<MemoryBuffer>>> |
| 351 | DWARFYAML::EmitDebugSections(StringRef YAMLString, bool ApplyFixups, |
| 352 | bool IsLittleEndian) { |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 353 | yaml::Input YIn(YAMLString); |
| 354 | |
| 355 | DWARFYAML::Data DI; |
| 356 | DI.IsLittleEndian = IsLittleEndian; |
| 357 | YIn >> DI; |
| 358 | if (YIn.error()) |
| 359 | return errorCodeToError(YIn.error()); |
| 360 | |
Jonas Devlieghere | 5c709ed | 2018-04-20 12:33:49 +0000 | [diff] [blame] | 361 | if (ApplyFixups) { |
| 362 | DIEFixupVisitor DIFixer(DI); |
| 363 | DIFixer.traverseDebugInfo(); |
| 364 | } |
| 365 | |
| 366 | StringMap<std::unique_ptr<MemoryBuffer>> DebugSections; |
Chris Bieneman | 2e752db | 2017-01-20 19:03:14 +0000 | [diff] [blame] | 367 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugInfo, "debug_info", |
| 368 | DebugSections); |
| 369 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugLine, "debug_line", |
| 370 | DebugSections); |
| 371 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugStr, "debug_str", |
| 372 | DebugSections); |
| 373 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAbbrev, "debug_abbrev", |
| 374 | DebugSections); |
| 375 | EmitDebugSectionImpl(DI, &DWARFYAML::EmitDebugAranges, "debug_aranges", |
| 376 | DebugSections); |
| 377 | return std::move(DebugSections); |
| 378 | } |