1 //===- llvm/unittest/DebugInfo/PDB/NativeSessionTest.cpp ------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/DebugInfo/PDB/Native/NativeSession.h"
10 #include "llvm/DebugInfo/PDB/ConcreteSymbolEnumerator.h"
11 #include "llvm/DebugInfo/PDB/IPDBSession.h"
12 #include "llvm/DebugInfo/PDB/PDB.h"
13 #include "llvm/DebugInfo/PDB/PDBSymbolFunc.h"
14 #include "llvm/DebugInfo/PDB/PDBSymbolPublicSymbol.h"
15 #include "llvm/DebugInfo/PDB/PDBSymbolTypeFunctionSig.h"
16 #include "llvm/Support/Path.h"
18 #include "llvm/Testing/Support/Error.h"
20 #include "gtest/gtest.h"
25 using namespace llvm::pdb
;
27 extern const char *TestMainArgv0
;
29 static std::string
getExePath() {
30 SmallString
<128> InputsDir
= unittest::getInputFileDirectory(TestMainArgv0
);
31 llvm::sys::path::append(InputsDir
, "SimpleTest.exe");
32 return std::string(InputsDir
);
35 TEST(NativeSessionTest
, TestCreateFromExe
) {
36 std::unique_ptr
<IPDBSession
> S
;
37 std::string ExePath
= getExePath();
38 Expected
<std::string
> PdbPath
= NativeSession::searchForPdb({ExePath
});
39 ASSERT_TRUE((bool)PdbPath
);
41 Error E
= NativeSession::createFromPdbPath(PdbPath
.get(), S
);
42 ASSERT_THAT_ERROR(std::move(E
), Succeeded());
45 TEST(NativeSessionTest
, TestSetLoadAddress
) {
46 std::unique_ptr
<IPDBSession
> S
;
47 Error E
= pdb::loadDataForEXE(PDB_ReaderType::Native
, getExePath(), S
);
48 ASSERT_THAT_ERROR(std::move(E
), Succeeded());
50 S
->setLoadAddress(123);
51 EXPECT_EQ(S
->getLoadAddress(), 123U);
54 TEST(NativeSessionTest
, TestAddressForVA
) {
55 std::unique_ptr
<IPDBSession
> S
;
56 Error E
= pdb::loadDataForEXE(PDB_ReaderType::Native
, getExePath(), S
);
57 ASSERT_THAT_ERROR(std::move(E
), Succeeded());
59 uint64_t LoadAddr
= S
->getLoadAddress();
62 ASSERT_TRUE(S
->addressForVA(LoadAddr
+ 5000, Section
, Offset
));
63 EXPECT_EQ(1U, Section
);
64 EXPECT_EQ(904U, Offset
);
66 ASSERT_TRUE(S
->addressForVA(-1, Section
, Offset
));
67 EXPECT_EQ(0U, Section
);
68 EXPECT_EQ(0U, Offset
);
70 ASSERT_TRUE(S
->addressForVA(4, Section
, Offset
));
71 EXPECT_EQ(0U, Section
);
72 EXPECT_EQ(4U, Offset
);
74 ASSERT_TRUE(S
->addressForVA(LoadAddr
+ 100000, Section
, Offset
));
75 EXPECT_EQ(3U, Section
);
76 EXPECT_EQ(83616U, Offset
);
79 TEST(NativeSessionTest
, TestAddressForRVA
) {
80 std::unique_ptr
<IPDBSession
> S
;
81 Error E
= pdb::loadDataForEXE(PDB_ReaderType::Native
, getExePath(), S
);
82 ASSERT_THAT_ERROR(std::move(E
), Succeeded());
86 ASSERT_TRUE(S
->addressForVA(5000, Section
, Offset
));
87 EXPECT_EQ(1U, Section
);
88 EXPECT_EQ(904U, Offset
);
90 ASSERT_TRUE(S
->addressForVA(-1, Section
, Offset
));
91 EXPECT_EQ(0U, Section
);
92 EXPECT_EQ(0U, Offset
);
94 ASSERT_TRUE(S
->addressForVA(4, Section
, Offset
));
95 EXPECT_EQ(0U, Section
);
96 EXPECT_EQ(4U, Offset
);
98 ASSERT_TRUE(S
->addressForVA(100000, Section
, Offset
));
99 EXPECT_EQ(3U, Section
);
100 EXPECT_EQ(83616U, Offset
);