1 //===-- MangledTest.cpp -----------------------------------------*- C++ -*-===//
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 "Plugins/ObjectFile/ELF/ObjectFileELF.h"
10 #include "Plugins/SymbolFile/Symtab/SymbolFileSymtab.h"
11 #include "TestingSupport/TestUtilities.h"
12 
13 #include "lldb/Core/Mangled.h"
14 #include "lldb/Core/Module.h"
15 #include "lldb/Core/ModuleSpec.h"
16 #include "lldb/Host/FileSystem.h"
17 #include "lldb/Host/HostInfo.h"
18 #include "lldb/Symbol/SymbolContext.h"
19 
20 #include "llvm/Support/FileUtilities.h"
21 #include "llvm/Support/Path.h"
22 #include "llvm/Support/Program.h"
23 #include "llvm/Testing/Support/Error.h"
24 
25 #include "gtest/gtest.h"
26 
27 using namespace lldb;
28 using namespace lldb_private;
29 
30 TEST(MangledTest, ResultForValidName) {
31   ConstString MangledName("_ZN1a1b1cIiiiEEvm");
32   bool IsMangled = true;
33 
34   Mangled TheMangled(MangledName, IsMangled);
35   ConstString TheDemangled =
36       TheMangled.GetDemangledName(eLanguageTypeC_plus_plus);
37 
38   ConstString ExpectedResult("void a::b::c<int, int, int>(unsigned long)");
39   EXPECT_STREQ(ExpectedResult.GetCString(), TheDemangled.GetCString());
40 }
41 
42 TEST(MangledTest, EmptyForInvalidName) {
43   ConstString MangledName("_ZN1a1b1cmxktpEEvm");
44   bool IsMangled = true;
45 
46   Mangled TheMangled(MangledName, IsMangled);
47   ConstString TheDemangled =
48       TheMangled.GetDemangledName(eLanguageTypeC_plus_plus);
49 
50   EXPECT_STREQ("", TheDemangled.GetCString());
51 }
52 
53 TEST(MangledTest, NameIndexes_FindFunctionSymbols) {
54   FileSystem::Initialize();
55   HostInfo::Initialize();
56   ObjectFileELF::Initialize();
57   SymbolFileSymtab::Initialize();
58 
59   auto ExpectedFile = TestFile::fromYaml(R"(
60 --- !ELF
61 FileHeader:
62   Class:           ELFCLASS64
63   Data:            ELFDATA2LSB
64   Type:            ET_EXEC
65   Machine:         EM_X86_64
66 Sections:
67   - Name:            .text
68     Type:            SHT_PROGBITS
69     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
70     AddressAlign:    0x0000000000000010
71     Size:            0x20
72   - Name:            .anothertext
73     Type:            SHT_PROGBITS
74     Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
75     Address:         0x0000000000000010
76     AddressAlign:    0x0000000000000010
77     Size:            0x40
78   - Name:            .data
79     Type:            SHT_PROGBITS
80     Flags:           [ SHF_WRITE, SHF_ALLOC ]
81     Address:         0x00000000000000A8
82     AddressAlign:    0x0000000000000004
83     Content:         '01000000'
84 Symbols:
85   - Name:            somedata
86     Type:            STT_OBJECT
87     Section:         .anothertext
88     Value:           0x0000000000000045
89     Binding:         STB_GLOBAL
90   - Name:            main
91     Type:            STT_FUNC
92     Section:         .anothertext
93     Value:           0x0000000000000010
94     Size:            0x000000000000003F
95     Binding:         STB_GLOBAL
96   - Name:            _Z3foov
97     Type:            STT_FUNC
98     Section:         .text
99     Size:            0x000000000000000D
100     Binding:         STB_GLOBAL
101   - Name:            puts@GLIBC_2.5
102     Type:            STT_FUNC
103     Section:         .text
104     Size:            0x000000000000000D
105     Binding:         STB_GLOBAL
106   - Name:            puts@GLIBC_2.6
107     Type:            STT_FUNC
108     Section:         .text
109     Size:            0x000000000000000D
110     Binding:         STB_GLOBAL
111   - Name:            _Z5annotv@VERSION3
112     Type:            STT_FUNC
113     Section:         .text
114     Size:            0x000000000000000D
115     Binding:         STB_GLOBAL
116   - Name:            _ZN1AC2Ev
117     Type:            STT_FUNC
118     Section:         .text
119     Size:            0x000000000000000D
120     Binding:         STB_GLOBAL
121   - Name:            _ZN1AD2Ev
122     Type:            STT_FUNC
123     Section:         .text
124     Size:            0x000000000000000D
125     Binding:         STB_GLOBAL
126   - Name:            _ZN1A3barEv
127     Type:            STT_FUNC
128     Section:         .text
129     Size:            0x000000000000000D
130     Binding:         STB_GLOBAL
131   - Name:            _ZGVZN4llvm4dbgsEvE7thestrm
132     Type:            STT_FUNC
133     Section:         .text
134     Size:            0x000000000000000D
135     Binding:         STB_GLOBAL
136   - Name:            _ZZN4llvm4dbgsEvE7thestrm
137     Type:            STT_FUNC
138     Section:         .text
139     Size:            0x000000000000000D
140     Binding:         STB_GLOBAL
141   - Name:            _ZTVN5clang4DeclE
142     Type:            STT_FUNC
143     Section:         .text
144     Size:            0x000000000000000D
145     Binding:         STB_GLOBAL
146   - Name:            -[ObjCfoo]
147     Type:            STT_FUNC
148     Section:         .text
149     Size:            0x000000000000000D
150     Binding:         STB_GLOBAL
151   - Name:            +[B ObjCbar(WithCategory)]
152     Type:            STT_FUNC
153     Section:         .text
154     Size:            0x000000000000000D
155     Binding:         STB_GLOBAL
156   - Name:            _Z12undemangableEvx42
157     Type:            STT_FUNC
158     Section:         .text
159     Size:            0x000000000000000D
160     Binding:         STB_GLOBAL
161 ...
162 )");
163   ASSERT_THAT_EXPECTED(ExpectedFile, llvm::Succeeded());
164 
165   ModuleSpec Spec{FileSpec(ExpectedFile->name())};
166   auto M = std::make_shared<Module>(Spec);
167 
168   auto Count = [M](const char *Name, FunctionNameType Type) -> int {
169     SymbolContextList SymList;
170     return M->FindFunctionSymbols(ConstString(Name), Type, SymList);
171   };
172 
173   // Unmangled
174   EXPECT_EQ(1, Count("main", eFunctionNameTypeFull));
175   EXPECT_EQ(1, Count("main", eFunctionNameTypeBase));
176   EXPECT_EQ(0, Count("main", eFunctionNameTypeMethod));
177 
178   // Itanium mangled
179   EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeFull));
180   EXPECT_EQ(1, Count("_Z3foov", eFunctionNameTypeBase));
181   EXPECT_EQ(1, Count("foo", eFunctionNameTypeBase));
182   EXPECT_EQ(0, Count("foo", eFunctionNameTypeMethod));
183 
184   // Unmangled with linker annotation
185   EXPECT_EQ(1, Count("puts@GLIBC_2.5", eFunctionNameTypeFull));
186   EXPECT_EQ(1, Count("puts@GLIBC_2.6", eFunctionNameTypeFull));
187   EXPECT_EQ(2, Count("puts", eFunctionNameTypeFull));
188   EXPECT_EQ(2, Count("puts", eFunctionNameTypeBase));
189   EXPECT_EQ(0, Count("puts", eFunctionNameTypeMethod));
190 
191   // Itanium mangled with linker annotation
192   EXPECT_EQ(1, Count("_Z5annotv@VERSION3", eFunctionNameTypeFull));
193   EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeFull));
194   EXPECT_EQ(1, Count("_Z5annotv", eFunctionNameTypeBase));
195   EXPECT_EQ(0, Count("annot", eFunctionNameTypeBase));
196   EXPECT_EQ(0, Count("annot", eFunctionNameTypeMethod));
197 
198   // Itanium mangled ctor A::A()
199   EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeFull));
200   EXPECT_EQ(1, Count("_ZN1AC2Ev", eFunctionNameTypeBase));
201   EXPECT_EQ(1, Count("A", eFunctionNameTypeMethod));
202   EXPECT_EQ(0, Count("A", eFunctionNameTypeBase));
203 
204   // Itanium mangled dtor A::~A()
205   EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeFull));
206   EXPECT_EQ(1, Count("_ZN1AD2Ev", eFunctionNameTypeBase));
207   EXPECT_EQ(1, Count("~A", eFunctionNameTypeMethod));
208   EXPECT_EQ(0, Count("~A", eFunctionNameTypeBase));
209 
210   // Itanium mangled method A::bar()
211   EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeFull));
212   EXPECT_EQ(1, Count("_ZN1A3barEv", eFunctionNameTypeBase));
213   EXPECT_EQ(1, Count("bar", eFunctionNameTypeMethod));
214   EXPECT_EQ(0, Count("bar", eFunctionNameTypeBase));
215 
216   // Itanium mangled names that are explicitly excluded from parsing
217   EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull));
218   EXPECT_EQ(1, Count("_ZGVZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase));
219   EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod));
220   EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase));
221   EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeFull));
222   EXPECT_EQ(1, Count("_ZZN4llvm4dbgsEvE7thestrm", eFunctionNameTypeBase));
223   EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeMethod));
224   EXPECT_EQ(0, Count("dbgs", eFunctionNameTypeBase));
225   EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeFull));
226   EXPECT_EQ(1, Count("_ZTVN5clang4DeclE", eFunctionNameTypeBase));
227   EXPECT_EQ(0, Count("Decl", eFunctionNameTypeMethod));
228   EXPECT_EQ(0, Count("Decl", eFunctionNameTypeBase));
229 
230   // ObjC mangled static
231   EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeFull));
232   EXPECT_EQ(1, Count("-[ObjCfoo]", eFunctionNameTypeBase));
233   EXPECT_EQ(0, Count("ObjCfoo", eFunctionNameTypeMethod));
234 
235   // ObjC mangled method with category
236   EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeFull));
237   EXPECT_EQ(1, Count("+[B ObjCbar(WithCategory)]", eFunctionNameTypeBase));
238   EXPECT_EQ(0, Count("ObjCbar", eFunctionNameTypeMethod));
239 
240   // Invalid things: unable to decode but still possible to find by full name
241   EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeFull));
242   EXPECT_EQ(1, Count("_Z12undemangableEvx42", eFunctionNameTypeBase));
243   EXPECT_EQ(0, Count("_Z12undemangableEvx42", eFunctionNameTypeMethod));
244   EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeBase));
245   EXPECT_EQ(0, Count("undemangable", eFunctionNameTypeMethod));
246 
247   SymbolFileSymtab::Terminate();
248   ObjectFileELF::Terminate();
249   HostInfo::Terminate();
250   FileSystem::Terminate();
251 }
252