1 //===- llvm/unittest/MC/DwarfLineTables.cpp ------------------------------===// 2 // 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 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/STLExtras.h" 10 #include "llvm/BinaryFormat/Dwarf.h" 11 #include "llvm/MC/MCAsmInfo.h" 12 #include "llvm/MC/MCContext.h" 13 #include "llvm/MC/MCDwarf.h" 14 #include "llvm/MC/MCRegisterInfo.h" 15 #include "llvm/Support/TargetRegistry.h" 16 #include "llvm/Support/TargetSelect.h" 17 #include "gtest/gtest.h" 18 19 using namespace llvm; 20 21 namespace { 22 struct Context { 23 const char *Triple = "x86_64-pc-linux"; 24 std::unique_ptr<MCRegisterInfo> MRI; 25 std::unique_ptr<MCAsmInfo> MAI; 26 std::unique_ptr<MCContext> Ctx; 27 28 Context() { 29 llvm::InitializeAllTargetInfos(); 30 llvm::InitializeAllTargetMCs(); 31 llvm::InitializeAllDisassemblers(); 32 33 // If we didn't build x86, do not run the test. 34 std::string Error; 35 const Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); 36 if (!TheTarget) 37 return; 38 39 MRI.reset(TheTarget->createMCRegInfo(Triple)); 40 MAI.reset(TheTarget->createMCAsmInfo(*MRI, Triple)); 41 Ctx = llvm::make_unique<MCContext>(MAI.get(), MRI.get(), nullptr); 42 } 43 44 operator bool() { return Ctx.get(); } 45 operator MCContext &() { return *Ctx; }; 46 }; 47 48 Context &getContext() { 49 static Context Ctxt; 50 return Ctxt; 51 } 52 } 53 54 void verifyEncoding(MCDwarfLineTableParams Params, int LineDelta, int AddrDelta, 55 ArrayRef<uint8_t> ExpectedEncoding) { 56 SmallString<16> Buffer; 57 raw_svector_ostream EncodingOS(Buffer); 58 MCDwarfLineAddr::Encode(getContext(), Params, LineDelta, AddrDelta, 59 EncodingOS); 60 ArrayRef<uint8_t> Encoding(reinterpret_cast<uint8_t *>(Buffer.data()), 61 Buffer.size()); 62 EXPECT_EQ(ExpectedEncoding, Encoding); 63 } 64 65 TEST(DwarfLineTables, TestDefaultParams) { 66 if (!getContext()) 67 return; 68 69 MCDwarfLineTableParams Params; 70 71 // Minimal line offset expressible through extended opcode, 0 addr delta 72 const uint8_t Encoding0[] = {13}; // Special opcode Addr += 0, Line += -5 73 verifyEncoding(Params, -5, 0, Encoding0); 74 75 // Maximal line offset expressible through extended opcode, 76 const uint8_t Encoding1[] = {26}; // Special opcode Addr += 0, Line += +8 77 verifyEncoding(Params, 8, 0, Encoding1); 78 79 // Random value in the middle of the special ocode range 80 const uint8_t Encoding2[] = {146}; // Special opcode Addr += 9, Line += 2 81 verifyEncoding(Params, 2, 9, Encoding2); 82 83 // Minimal line offset expressible through extended opcode, max addr delta 84 const uint8_t Encoding3[] = {251}; // Special opcode Addr += 17, Line += -5 85 verifyEncoding(Params, -5, 17, Encoding3); 86 87 // Biggest special opcode 88 const uint8_t Encoding4[] = {255}; // Special opcode Addr += 17, Line += -1 89 verifyEncoding(Params, -1, 17, Encoding4); 90 91 // Line delta outside of the special opcode range, address delta in range 92 const uint8_t Encoding5[] = {dwarf::DW_LNS_advance_line, 9, 93 158}; // Special opcode Addr += 10, Line += 0 94 verifyEncoding(Params, 9, 10, Encoding5); 95 96 // Address delta outside of the special opcode range, but small 97 // enough to do DW_LNS_const_add_pc + special opcode. 98 const uint8_t Encoding6[] = {dwarf::DW_LNS_const_add_pc, // pc += 17 99 62}; // Special opcode Addr += 3, Line += 2 100 verifyEncoding(Params, 2, 20, Encoding6); 101 102 // Address delta big enough to require the use of DW_LNS_advance_pc 103 // Line delta in special opcode range 104 const uint8_t Encoding7[] = {dwarf::DW_LNS_advance_pc, 100, 105 20}; // Special opcode Addr += 0, Line += 2 106 verifyEncoding(Params, 2, 100, Encoding7); 107 108 // No special opcode possible. 109 const uint8_t Encoding8[] = {dwarf::DW_LNS_advance_line, 20, 110 dwarf::DW_LNS_advance_pc, 100, 111 dwarf::DW_LNS_copy}; 112 verifyEncoding(Params, 20, 100, Encoding8); 113 } 114 115 TEST(DwarfLineTables, TestCustomParams) { 116 if (!getContext()) 117 return; 118 119 // Some tests against the example values given in the standard. 120 MCDwarfLineTableParams Params; 121 Params.DWARF2LineOpcodeBase = 13; 122 Params.DWARF2LineBase = -3; 123 Params.DWARF2LineRange = 12; 124 125 // Minimal line offset expressible through extended opcode, 0 addr delta 126 const uint8_t Encoding0[] = {13}; // Special opcode Addr += 0, Line += -5 127 verifyEncoding(Params, -3, 0, Encoding0); 128 129 // Maximal line offset expressible through extended opcode, 130 const uint8_t Encoding1[] = {24}; // Special opcode Addr += 0, Line += +8 131 verifyEncoding(Params, 8, 0, Encoding1); 132 133 // Random value in the middle of the special ocode range 134 const uint8_t Encoding2[] = {126}; // Special opcode Addr += 9, Line += 2 135 verifyEncoding(Params, 2, 9, Encoding2); 136 137 // Minimal line offset expressible through extended opcode, max addr delta 138 const uint8_t Encoding3[] = {253}; // Special opcode Addr += 20, Line += -3 139 verifyEncoding(Params, -3, 20, Encoding3); 140 141 // Biggest special opcode 142 const uint8_t Encoding4[] = {255}; // Special opcode Addr += 17, Line += -1 143 verifyEncoding(Params, -1, 20, Encoding4); 144 145 // Line delta outside of the special opcode range, address delta in range 146 const uint8_t Encoding5[] = {dwarf::DW_LNS_advance_line, 9, 147 136}; // Special opcode Addr += 10, Line += 0 148 verifyEncoding(Params, 9, 10, Encoding5); 149 150 // Address delta outside of the special opcode range, but small 151 // enough to do DW_LNS_const_add_pc + special opcode. 152 const uint8_t Encoding6[] = {dwarf::DW_LNS_const_add_pc, // pc += 20 153 138}; // Special opcode Addr += 10, Line += 2 154 verifyEncoding(Params, 2, 30, Encoding6); 155 156 // Address delta big enough to require the use of DW_LNS_advance_pc 157 // Line delta in special opcode range 158 const uint8_t Encoding7[] = {dwarf::DW_LNS_advance_pc, 100, 159 18}; // Special opcode Addr += 0, Line += 2 160 verifyEncoding(Params, 2, 100, Encoding7); 161 162 // No special opcode possible. 163 const uint8_t Encoding8[] = {dwarf::DW_LNS_advance_line, 20, 164 dwarf::DW_LNS_advance_pc, 100, 165 dwarf::DW_LNS_copy}; 166 verifyEncoding(Params, 20, 100, Encoding8); 167 } 168 169 TEST(DwarfLineTables, TestCustomParams2) { 170 if (!getContext()) 171 return; 172 173 // Corner case param values. 174 MCDwarfLineTableParams Params; 175 Params.DWARF2LineOpcodeBase = 13; 176 Params.DWARF2LineBase = 1; 177 Params.DWARF2LineRange = 255; 178 179 const uint8_t Encoding0[] = {dwarf::DW_LNS_advance_line, 248, 1, 180 dwarf::DW_LNS_copy}; 181 verifyEncoding(Params, 248, 0, Encoding0); 182 } 183