AsmPrinterDwarfTest.cpp
12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
//===- llvm/unittest/CodeGen/AsmPrinterDwarfTest.cpp ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "TestAsmPrinter.h"
#include "llvm/CodeGen/AsmPrinter.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCSectionELF.h"
#include "llvm/Testing/Support/Error.h"
using namespace llvm;
using testing::_;
using testing::InSequence;
using testing::SaveArg;
namespace {
class AsmPrinterFixtureBase : public testing::Test {
void setupTestPrinter(const std::string &TripleStr, unsigned DwarfVersion,
dwarf::DwarfFormat DwarfFormat) {
auto ExpectedTestPrinter =
TestAsmPrinter::create(TripleStr, DwarfVersion, DwarfFormat);
ASSERT_THAT_EXPECTED(ExpectedTestPrinter, Succeeded());
TestPrinter = std::move(ExpectedTestPrinter.get());
}
protected:
bool init(const std::string &TripleStr, unsigned DwarfVersion,
dwarf::DwarfFormat DwarfFormat) {
setupTestPrinter(TripleStr, DwarfVersion, DwarfFormat);
return TestPrinter != nullptr;
}
std::unique_ptr<TestAsmPrinter> TestPrinter;
};
class AsmPrinterEmitDwarfSymbolReferenceTest : public AsmPrinterFixtureBase {
protected:
bool init(const std::string &TripleStr, unsigned DwarfVersion,
dwarf::DwarfFormat DwarfFormat) {
if (!AsmPrinterFixtureBase::init(TripleStr, DwarfVersion, DwarfFormat))
return false;
// Create a symbol which will be emitted in the tests and associate it
// with a section because that is required in some code paths.
Val = TestPrinter->getCtx().createTempSymbol();
Sec = TestPrinter->getCtx().getELFSection(".tst", ELF::SHT_PROGBITS, 0);
SecBeginSymbol = Sec->getBeginSymbol();
TestPrinter->getMS().SwitchSection(Sec);
TestPrinter->getMS().emitLabel(Val);
return true;
}
MCSymbol *Val = nullptr;
MCSection *Sec = nullptr;
MCSymbol *SecBeginSymbol = nullptr;
};
TEST_F(AsmPrinterEmitDwarfSymbolReferenceTest, COFF) {
if (!init("x86_64-pc-windows", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
EXPECT_CALL(TestPrinter->getMS(), EmitCOFFSecRel32(Val, 0));
TestPrinter->getAP()->emitDwarfSymbolReference(Val, false);
}
TEST_F(AsmPrinterEmitDwarfSymbolReferenceTest, COFFForceOffset) {
if (!init("x86_64-pc-windows", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
EXPECT_CALL(TestPrinter->getMS(),
emitAbsoluteSymbolDiff(Val, SecBeginSymbol, 4));
TestPrinter->getAP()->emitDwarfSymbolReference(Val, true);
}
TEST_F(AsmPrinterEmitDwarfSymbolReferenceTest, ELFDWARF32) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
const MCExpr *Arg0 = nullptr;
EXPECT_CALL(TestPrinter->getMS(), emitValueImpl(_, 4, _))
.WillOnce(SaveArg<0>(&Arg0));
TestPrinter->getAP()->emitDwarfSymbolReference(Val, false);
const MCSymbolRefExpr *ActualArg0 = dyn_cast_or_null<MCSymbolRefExpr>(Arg0);
ASSERT_NE(ActualArg0, nullptr);
EXPECT_EQ(&(ActualArg0->getSymbol()), Val);
}
TEST_F(AsmPrinterEmitDwarfSymbolReferenceTest, ELFDWARF32ForceOffset) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
EXPECT_CALL(TestPrinter->getMS(),
emitAbsoluteSymbolDiff(Val, SecBeginSymbol, 4));
TestPrinter->getAP()->emitDwarfSymbolReference(Val, true);
}
TEST_F(AsmPrinterEmitDwarfSymbolReferenceTest, ELFDWARF64) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
const MCExpr *Arg0 = nullptr;
EXPECT_CALL(TestPrinter->getMS(), emitValueImpl(_, 8, _))
.WillOnce(SaveArg<0>(&Arg0));
TestPrinter->getAP()->emitDwarfSymbolReference(Val, false);
const MCSymbolRefExpr *ActualArg0 = dyn_cast_or_null<MCSymbolRefExpr>(Arg0);
ASSERT_NE(ActualArg0, nullptr);
EXPECT_EQ(&(ActualArg0->getSymbol()), Val);
}
TEST_F(AsmPrinterEmitDwarfSymbolReferenceTest, ELFDWARF64ForceOffset) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
EXPECT_CALL(TestPrinter->getMS(),
emitAbsoluteSymbolDiff(Val, SecBeginSymbol, 8));
TestPrinter->getAP()->emitDwarfSymbolReference(Val, true);
}
class AsmPrinterEmitDwarfStringOffsetTest : public AsmPrinterFixtureBase {
protected:
bool init(const std::string &TripleStr, unsigned DwarfVersion,
dwarf::DwarfFormat DwarfFormat) {
if (!AsmPrinterFixtureBase::init(TripleStr, DwarfVersion, DwarfFormat))
return false;
Val.Index = DwarfStringPoolEntry::NotIndexed;
Val.Symbol = TestPrinter->getCtx().createTempSymbol();
Val.Offset = 42;
return true;
}
DwarfStringPoolEntry Val;
};
TEST_F(AsmPrinterEmitDwarfStringOffsetTest, DWARF32) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
const MCExpr *Arg0 = nullptr;
EXPECT_CALL(TestPrinter->getMS(), emitValueImpl(_, 4, _))
.WillOnce(SaveArg<0>(&Arg0));
TestPrinter->getAP()->emitDwarfStringOffset(Val);
const MCSymbolRefExpr *ActualArg0 = dyn_cast_or_null<MCSymbolRefExpr>(Arg0);
ASSERT_NE(ActualArg0, nullptr);
EXPECT_EQ(&(ActualArg0->getSymbol()), Val.Symbol);
}
TEST_F(AsmPrinterEmitDwarfStringOffsetTest,
DWARF32NoRelocationsAcrossSections) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
TestPrinter->setDwarfUsesRelocationsAcrossSections(false);
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(Val.Offset, 4));
TestPrinter->getAP()->emitDwarfStringOffset(Val);
}
TEST_F(AsmPrinterEmitDwarfStringOffsetTest, DWARF64) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
const MCExpr *Arg0 = nullptr;
EXPECT_CALL(TestPrinter->getMS(), emitValueImpl(_, 8, _))
.WillOnce(SaveArg<0>(&Arg0));
TestPrinter->getAP()->emitDwarfStringOffset(Val);
const MCSymbolRefExpr *ActualArg0 = dyn_cast_or_null<MCSymbolRefExpr>(Arg0);
ASSERT_NE(ActualArg0, nullptr);
EXPECT_EQ(&(ActualArg0->getSymbol()), Val.Symbol);
}
TEST_F(AsmPrinterEmitDwarfStringOffsetTest,
DWARF64NoRelocationsAcrossSections) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
TestPrinter->setDwarfUsesRelocationsAcrossSections(false);
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(Val.Offset, 8));
TestPrinter->getAP()->emitDwarfStringOffset(Val);
}
class AsmPrinterEmitDwarfOffsetTest : public AsmPrinterFixtureBase {
protected:
bool init(const std::string &TripleStr, unsigned DwarfVersion,
dwarf::DwarfFormat DwarfFormat) {
if (!AsmPrinterFixtureBase::init(TripleStr, DwarfVersion, DwarfFormat))
return false;
Label = TestPrinter->getCtx().createTempSymbol();
return true;
}
MCSymbol *Label = nullptr;
uint64_t Offset = 42;
};
TEST_F(AsmPrinterEmitDwarfOffsetTest, DWARF32) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
const MCExpr *Arg0 = nullptr;
EXPECT_CALL(TestPrinter->getMS(), emitValueImpl(_, 4, _))
.WillOnce(SaveArg<0>(&Arg0));
TestPrinter->getAP()->emitDwarfOffset(Label, Offset);
const MCBinaryExpr *ActualArg0 = dyn_cast_or_null<MCBinaryExpr>(Arg0);
ASSERT_NE(ActualArg0, nullptr);
EXPECT_EQ(ActualArg0->getOpcode(), MCBinaryExpr::Add);
const MCSymbolRefExpr *ActualLHS =
dyn_cast_or_null<MCSymbolRefExpr>(ActualArg0->getLHS());
ASSERT_NE(ActualLHS, nullptr);
EXPECT_EQ(&(ActualLHS->getSymbol()), Label);
const MCConstantExpr *ActualRHS =
dyn_cast_or_null<MCConstantExpr>(ActualArg0->getRHS());
ASSERT_NE(ActualRHS, nullptr);
EXPECT_EQ(static_cast<uint64_t>(ActualRHS->getValue()), Offset);
}
TEST_F(AsmPrinterEmitDwarfOffsetTest, DWARF64) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
const MCExpr *Arg0 = nullptr;
EXPECT_CALL(TestPrinter->getMS(), emitValueImpl(_, 8, _))
.WillOnce(SaveArg<0>(&Arg0));
TestPrinter->getAP()->emitDwarfOffset(Label, Offset);
const MCBinaryExpr *ActualArg0 = dyn_cast_or_null<MCBinaryExpr>(Arg0);
ASSERT_NE(ActualArg0, nullptr);
EXPECT_EQ(ActualArg0->getOpcode(), MCBinaryExpr::Add);
const MCSymbolRefExpr *ActualLHS =
dyn_cast_or_null<MCSymbolRefExpr>(ActualArg0->getLHS());
ASSERT_NE(ActualLHS, nullptr);
EXPECT_EQ(&(ActualLHS->getSymbol()), Label);
const MCConstantExpr *ActualRHS =
dyn_cast_or_null<MCConstantExpr>(ActualArg0->getRHS());
ASSERT_NE(ActualRHS, nullptr);
EXPECT_EQ(static_cast<uint64_t>(ActualRHS->getValue()), Offset);
}
class AsmPrinterEmitDwarfLengthOrOffsetTest : public AsmPrinterFixtureBase {
protected:
uint64_t Val = 42;
};
TEST_F(AsmPrinterEmitDwarfLengthOrOffsetTest, DWARF32) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(Val, 4));
TestPrinter->getAP()->emitDwarfLengthOrOffset(Val);
}
TEST_F(AsmPrinterEmitDwarfLengthOrOffsetTest, DWARF64) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(Val, 8));
TestPrinter->getAP()->emitDwarfLengthOrOffset(Val);
}
class AsmPrinterGetUnitLengthFieldByteSizeTest : public AsmPrinterFixtureBase {
};
TEST_F(AsmPrinterGetUnitLengthFieldByteSizeTest, DWARF32) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
EXPECT_EQ(TestPrinter->getAP()->getUnitLengthFieldByteSize(), 4u);
}
TEST_F(AsmPrinterGetUnitLengthFieldByteSizeTest, DWARF64) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
EXPECT_EQ(TestPrinter->getAP()->getUnitLengthFieldByteSize(), 12u);
}
class AsmPrinterMaybeEmitDwarf64MarkTest : public AsmPrinterFixtureBase {};
TEST_F(AsmPrinterMaybeEmitDwarf64MarkTest, DWARF32) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(_, _)).Times(0);
TestPrinter->getAP()->maybeEmitDwarf64Mark();
}
TEST_F(AsmPrinterMaybeEmitDwarf64MarkTest, DWARF64) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(dwarf::DW_LENGTH_DWARF64, 4));
TestPrinter->getAP()->maybeEmitDwarf64Mark();
}
class AsmPrinterEmitDwarfUnitLengthAsIntTest : public AsmPrinterFixtureBase {
protected:
uint64_t Val = 42;
};
TEST_F(AsmPrinterEmitDwarfUnitLengthAsIntTest, DWARF32) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(Val, 4));
TestPrinter->getAP()->emitDwarfUnitLength(Val, "");
}
TEST_F(AsmPrinterEmitDwarfUnitLengthAsIntTest, DWARF64) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
InSequence S;
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(dwarf::DW_LENGTH_DWARF64, 4));
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(Val, 8));
TestPrinter->getAP()->emitDwarfUnitLength(Val, "");
}
class AsmPrinterEmitDwarfUnitLengthAsHiLoDiffTest
: public AsmPrinterFixtureBase {
protected:
bool init(const std::string &TripleStr, unsigned DwarfVersion,
dwarf::DwarfFormat DwarfFormat) {
if (!AsmPrinterFixtureBase::init(TripleStr, DwarfVersion, DwarfFormat))
return false;
Hi = TestPrinter->getCtx().createTempSymbol();
Lo = TestPrinter->getCtx().createTempSymbol();
return true;
}
MCSymbol *Hi = nullptr;
MCSymbol *Lo = nullptr;
};
TEST_F(AsmPrinterEmitDwarfUnitLengthAsHiLoDiffTest, DWARF32) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF32))
return;
EXPECT_CALL(TestPrinter->getMS(), emitAbsoluteSymbolDiff(Hi, Lo, 4));
TestPrinter->getAP()->emitDwarfUnitLength(Hi, Lo, "");
}
TEST_F(AsmPrinterEmitDwarfUnitLengthAsHiLoDiffTest, DWARF64) {
if (!init("x86_64-pc-linux", /*DwarfVersion=*/4, dwarf::DWARF64))
return;
InSequence S;
EXPECT_CALL(TestPrinter->getMS(), emitIntValue(dwarf::DW_LENGTH_DWARF64, 4));
EXPECT_CALL(TestPrinter->getMS(), emitAbsoluteSymbolDiff(Hi, Lo, 8));
TestPrinter->getAP()->emitDwarfUnitLength(Hi, Lo, "");
}
} // end namespace