-
Notifications
You must be signed in to change notification settings - Fork 5.6k
/
RustDemangler.java
132 lines (107 loc) · 3.55 KB
/
RustDemangler.java
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
/* ###
* IP: GHIDRA
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ghidra.app.plugin.core.analysis.rust.demangler;
import ghidra.app.plugin.core.analysis.rust.RustUtilities;
import ghidra.app.util.demangler.*;
import ghidra.program.model.lang.CompilerSpec;
import ghidra.program.model.listing.Program;
/**
* A class for demangling debug symbols created using rustc
*/
public class RustDemangler implements Demangler {
public RustDemangler() {
// needed to instantiate dynamically
}
@Override
public DemanglerOptions createDefaultOptions() {
return new RustDemanglerOptions();
}
@Override
public boolean canDemangle(Program program) {
return RustUtilities.isRustProgram(program);
}
@Override
@Deprecated(since = "9.2", forRemoval = true)
public DemangledObject demangle(String mangled, boolean demangleOnlyKnownPatterns)
throws DemangledException {
return null;
}
@Override
public DemangledObject demangle(String mangled, DemanglerOptions options) {
RustDemanglerOptions rustOptions = getRustOptions(options);
if (skip(mangled, rustOptions)) {
return null;
}
String demangled = null;
if (rustOptions.getDemanglerFormat() == RustDemanglerFormat.LEGACY ||
rustOptions.getDemanglerFormat() == RustDemanglerFormat.AUTO) {
demangled = RustDemanglerLegacy.demangle(mangled);
}
if (rustOptions.getDemanglerFormat() == RustDemanglerFormat.V0 ||
(rustOptions.getDemanglerFormat() == RustDemanglerFormat.AUTO && demangled == null)) {
demangled = RustDemanglerV0.demangle(mangled);
}
RustDemanglerParser parser = new RustDemanglerParser();
DemangledObject demangledObject = parser.parse(mangled, demangled);
if (options.applyCallingConvention() &&
demangledObject instanceof DemangledFunction demangledFunction) {
demangledFunction.setCallingConvention(CompilerSpec.CALLING_CONVENTION_rustcall);
}
return demangledObject;
}
private RustDemanglerOptions getRustOptions(DemanglerOptions options) {
if (options instanceof RustDemanglerOptions) {
return (RustDemanglerOptions) options;
}
return new RustDemanglerOptions(options);
}
/**
* Determines if the given mangled string should not be demangled, on the basis
* of if it has a known start pattern
*
* @param mangled the mangled string
* @param options the options
* @return true if the string should not be demangled
*/
private boolean skip(String mangled, RustDemanglerOptions options) {
// The current list of demangler start patterns
if (!options.demangleOnlyKnownPatterns()) {
return false;
}
return !isRustMangled(mangled);
}
/**
* Return true if the string is a mangled rust string in a rust program
*
* @param mangled potential mangled string
* @return true if the string could be a mangled string in a rust program
*/
public static boolean isRustMangled(String mangled) {
if (mangled.startsWith("_ZN")) {
return true;
}
if (mangled.startsWith("__ZN")) {
return true;
}
if (mangled.startsWith("_R")) {
return true;
}
if (mangled.startsWith("__R")) {
return true;
}
return false;
}
}