Skip to content

Commit 65d0a3e

Browse files
committed
2 parents 83653f2 + 26b0d85 commit 65d0a3e

18 files changed

+199
-13
lines changed

basicblock.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,18 @@ void DisassemblySettings::SetGutterWidth(size_t width)
9090
}
9191

9292

93+
BNDisassemblyAddressMode DisassemblySettings::GetAddressMode() const
94+
{
95+
return BNGetDisassemblyAddressMode(m_object);
96+
}
97+
98+
99+
void DisassemblySettings::SetAddressMode(BNDisassemblyAddressMode mode)
100+
{
101+
BNSetDisassemblyAddressMode(m_object, mode);
102+
}
103+
104+
93105
DisassemblyTextLine::DisassemblyTextLine()
94106
{
95107
addr = 0;

binaryninjaapi.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,6 +1531,26 @@ namespace BinaryNinja {
15311531
std::function<bool(size_t, size_t)> progress = {}, Ref<Metadata> options = new Metadata(MetadataType::KeyValueDataType),
15321532
bool isDatabase = false);
15331533

1534+
/*! Demangles using LLVM's demangler
1535+
1536+
\param[in] mangledName a mangled (msvc/itanium/rust/dlang) name
1537+
\param[out] outVarName QualifiedName reference to write the output name to.
1538+
\param[in] simplify Whether to simplify demangled names.
1539+
1540+
\ingroup demangle
1541+
*/
1542+
bool DemangleLLVM(const std::string& mangledName, QualifiedName& outVarName, const bool simplify = false);
1543+
1544+
/*! Demangles using LLVM's demangler
1545+
1546+
\param[in] mangledName a mangled (msvc/itanium/rust/dlang) name
1547+
\param[out] outVarName QualifiedName reference to write the output name to.
1548+
\param[in] view View to check the analysis.types.templateSimplifier for
1549+
1550+
\ingroup demangle
1551+
*/
1552+
bool DemangleLLVM(const std::string& mangledName, QualifiedName& outVarName, BinaryView* view);
1553+
15341554
/*! Demangles a Microsoft Visual Studio C++ name
15351555

15361556
\param[in] arch Architecture for the symbol. Required for pointer and integer sizes.
@@ -2266,6 +2286,7 @@ namespace BinaryNinja {
22662286
TextToken Used for anything not of another type.
22672287
CommentToken Comments
22682288
TypeNameToken **Not emitted by architectures**
2289+
AddressSeparatorToken **Not emitted by architectures**
22692290
========================== ============================================
22702291
*/
22712292
struct InstructionTextToken
@@ -9699,6 +9720,8 @@ namespace BinaryNinja {
96999720
void SetMaximumSymbolWidth(size_t width);
97009721
size_t GetGutterWidth() const;
97019722
void SetGutterWidth(size_t width);
9723+
BNDisassemblyAddressMode GetAddressMode() const;
9724+
void SetAddressMode(BNDisassemblyAddressMode mode);
97029725
};
97039726

97049727
/*!

binaryninjacore.h

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
// Current ABI version for linking to the core. This is incremented any time
3838
// there are changes to the API that affect linking, including new functions,
3939
// new types, or modifications to existing functions or types.
40-
#define BN_CURRENT_CORE_ABI_VERSION 60
40+
#define BN_CURRENT_CORE_ABI_VERSION 61
4141

4242
// Minimum ABI version that is supported for loading of plugins. Plugins that
4343
// are linked to an ABI version less than this will not be able to load and
@@ -382,7 +382,8 @@ extern "C"
382382
AddressDisplayToken = 68,
383383
IndirectImportToken = 69,
384384
ExternalSymbolToken = 70,
385-
StackVariableToken = 71
385+
StackVariableToken = 71,
386+
AddressSeparatorToken = 72
386387
} BNInstructionTextTokenType;
387388

388389
typedef enum BNInstructionTextTokenContext
@@ -700,6 +701,20 @@ extern "C"
700701
ShowILOpcodes = 131,
701702
} BNDisassemblyOption;
702703

704+
typedef enum BNDisassemblyAddressMode
705+
{
706+
AbsoluteDisassemblyAddressMode,
707+
RelativeToBinaryStartDisassemblyAddressMode,
708+
RelativeToSegmentStartDisassemblyAddressMode,
709+
RelativeToSectionStartDisassemblyAddressMode,
710+
RelativeToFunctionStartDisassemblyAddressMode,
711+
DisassemblyAddressModeMask = 0xFFFF,
712+
713+
IncludeNameDisassemblyAddressModeFlag = 0x10000,
714+
DecimalDisassemblyAddressModeFlag = 0x20000,
715+
DisassemblyAddressModeFlagsMask = 0xFFFF0000,
716+
} BNDisassemblyAddressMode;
717+
703718
typedef enum BNTypeClass
704719
{
705720
VoidTypeClass = 0,
@@ -1902,6 +1917,8 @@ extern "C"
19021917
CommentColor,
19031918
OperationColor,
19041919
BaseStructureNameColor,
1920+
IndentationLineColor,
1921+
IndentationLineHighlightColor,
19051922

19061923
// Script console colors
19071924
ScriptConsoleOutputColor,
@@ -5060,7 +5077,8 @@ extern "C"
50605077
BINARYNINJACOREAPI void BNSetDisassemblyMaximumSymbolWidth(BNDisassemblySettings* settings, size_t width);
50615078
BINARYNINJACOREAPI size_t BNGetDisassemblyGutterWidth(BNDisassemblySettings* settings);
50625079
BINARYNINJACOREAPI void BNSetDisassemblyGutterWidth(BNDisassemblySettings* settings, size_t width);
5063-
5080+
BINARYNINJACOREAPI BNDisassemblyAddressMode BNGetDisassemblyAddressMode(BNDisassemblySettings* settings);
5081+
BINARYNINJACOREAPI void BNSetDisassemblyAddressMode(BNDisassemblySettings* settings, BNDisassemblyAddressMode mode);
50645082

50655083
// Flow graphs
50665084
BINARYNINJACOREAPI BNFlowGraph* BNCreateFlowGraph(void);
@@ -6515,6 +6533,11 @@ extern "C"
65156533
char*** outVarName, size_t* outVarNameElements, const BNBinaryView* const view);
65166534
BINARYNINJACOREAPI void BNFreeDemangledName(char*** name, size_t nameElements);
65176535

6536+
BINARYNINJACOREAPI bool BNDemangleLLVM(const char* mangledName,
6537+
char*** outVarName, size_t* outVarNameElements, const bool simplify);
6538+
BINARYNINJACOREAPI bool BNDemangleLLVMWithOptions(const char* mangledName,
6539+
char*** outVarName, size_t* outVarNameElements, const BNBinaryView* const view);
6540+
65186541
// Plugin repository APIs
65196542
BINARYNINJACOREAPI char** BNPluginGetApis(BNRepoPlugin* p, size_t* count);
65206543
BINARYNINJACOREAPI const char* BNPluginGetAuthor(BNRepoPlugin* p);

demangle.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,29 @@
33
using namespace std;
44

55
namespace BinaryNinja {
6+
bool DemangleLLVM(const std::string& mangledName, QualifiedName& outVarName,
7+
BinaryView* view)
8+
{
9+
const bool simplify = Settings::Instance()->Get<bool>("analysis.types.templateSimplifier", view);
10+
return DemangleLLVM(mangledName, outVarName, simplify);
11+
}
12+
13+
bool DemangleLLVM(const std::string& mangledName, QualifiedName& outVarName,
14+
const bool simplify)
15+
{
16+
BNType* localType = nullptr;
17+
char** localVarName = nullptr;
18+
size_t localSize = 0;
19+
if (!BNDemangleLLVM(mangledName.c_str(), &localVarName, &localSize, simplify))
20+
return false;
21+
for (size_t i = 0; i < localSize; i++)
22+
{
23+
outVarName.push_back(localVarName[i]);
24+
}
25+
BNFreeDemangledName(&localVarName, localSize);
26+
return true;
27+
}
28+
629
bool DemangleMS(Architecture* arch, const std::string& mangledName, Ref<Type>& outType, QualifiedName& outVarName,
730
BinaryView* view)
831
{

docs/guide/settings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ All settings are uniquely identified with an identifier string. Identifiers are
360360
|ui|Random Theme on Startup|Randomize the theme on application startup.|`boolean`|`False`|[`SettingsUserScope`]|<a id='ui.theme.randomize'>ui.theme.randomize</a>|
361361
|ui|Match Types by Substring|When changing a type in the change type dialog, if this setting is enabled all substrings will be matched instead of only startsWith style matching|`boolean`|`False`|[`SettingsUserScope`]|<a id='ui.types.substring'>ui.types.substring</a>|
362362
|ui|Comment Width|Maximum width of comment before wrapping, in characters. A value of 0x0 means no wrapping.|`number`|`80`|[`SettingsUserScope`]|<a id='ui.view.common.commentWidth'>ui.view.common.commentWidth</a>|
363+
|ui|Minimum Comment Spacing|Minimum amount of characters from the beginning of line before showing comments|`number`|`0`|[`SettingsUserScope`]|<a id='ui.view.common.minimumCommentSpacing'>ui.view.common.minimumCommentSpacing</a>|
363364
|ui|Disassembly Width|Maximum width of disassembly output, in characters. Not used in cases where disassembly width is automatically calculated, e.g. Linear View.|`number`|`80`|[`SettingsUserScope`]|<a id='ui.view.common.disassemblyWidth'>ui.view.common.disassemblyWidth</a>|
364365
|ui|Maximum Symbol Name Length|Maximum allowed length of symbol names (in characters) before truncation is used.|`number`|`64`|[`SettingsUserScope`]|<a id='ui.view.common.maxSymbolWidth'>ui.view.common.maxSymbolWidth</a>|
365366
|ui|Graph View IL Carousel|Specify the IL view types and order for use with the 'Cycle IL' actions in Graph view.|`array`|[`Disassembly`, `HighLevelIL`, `LowLevelIL`, `MediumLevelIL`]|[`SettingsUserScope`]|<a id='ui.view.graph.carousel'>ui.view.graph.carousel</a>|

python/architecture.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2808,6 +2808,7 @@ class InstructionTextToken:
28082808
TextToken Used for anything not of another type.
28092809
CommentToken Comments
28102810
TypeNameToken **Not emitted by architectures**
2811+
AddressSeparatorToken **Not emitted by architectures**
28112812
========================== ============================================
28122813
28132814
"""

python/basedetection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ def aborted(self) -> bool:
161161
def detect_base_address(
162162
self,
163163
arch: Optional[str] = "",
164-
analysis: Optional[str] = Literal["basic", "controlFlow", "full"],
164+
analysis: Optional[Literal["basic", "controlFlow", "full"]] = "full",
165165
min_strlen: Optional[int] = 10,
166166
alignment: Optional[int] = 1024,
167167
low_boundary: Optional[int] = 0,

python/binaryview.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4463,8 +4463,6 @@ def get_functions_by_name(
44634463
SymbolType.FunctionSymbol, SymbolType.ImportedFunctionSymbol, SymbolType.LibraryFunctionSymbol
44644464
]
44654465

4466-
if plat == None:
4467-
plat = self.platform
44684466
fns = []
44694467
addresses = [sym.address for sym in self.get_symbols_by_name(name, ordered_filter=ordered_filter)]
44704468
if len(addresses) == 0 and name.startswith("sub_"):
@@ -4474,7 +4472,9 @@ def get_functions_by_name(
44744472
addresses = []
44754473
for address in addresses:
44764474
for fn in self.get_functions_at(address):
4477-
if fn.start == address and fn.platform == plat:
4475+
if fn.start == address:
4476+
if plat is not None and fn.platform != plat:
4477+
continue
44784478
fns.append(fn)
44794479
return fns
44804480

python/demangle.py

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@ def get_qualified_name(names):
4646
return "::".join(names)
4747

4848

49+
def demangle_llvm(mangled_name: str, options=None):
50+
"""
51+
``demangle_llvm`` demangles a mangled name to a Type object.
52+
53+
:param str mangled_name: a mangled (msvc/itanium/rust/dlang) name
54+
:param options: (optional) Whether to simplify demangled names : None falls back to user settings, a BinaryView uses that BinaryView's settings, or a boolean to set it directly
55+
:type options: Tuple[bool, BinaryView, None]
56+
:return: returns demangled name or None on error
57+
:rtype: str
58+
"""
59+
outName = ctypes.POINTER(ctypes.c_char_p)()
60+
outSize = ctypes.c_ulonglong()
61+
names = []
62+
if (
63+
isinstance(options, binaryview.BinaryView) and core.BNDemangleLLVMWithOptions(
64+
mangled_name, ctypes.byref(outName), ctypes.byref(outSize), options.handle
65+
)
66+
) or (
67+
isinstance(options, bool) and core.BNDemangleLLVM(
68+
mangled_name, ctypes.byref(outName), ctypes.byref(outSize), options
69+
)
70+
) or (
71+
options is None and core.BNDemangleLLVMWithOptions(
72+
mangled_name, ctypes.byref(outName), ctypes.byref(outSize), None
73+
)
74+
):
75+
for i in range(outSize.value):
76+
names.append(outName[i].decode('utf8')) # type: ignore
77+
core.BNFreeDemangledName(ctypes.byref(outName), outSize.value)
78+
return names
79+
return None
80+
81+
4982
def demangle_ms(archOrPlatform:Union[Architecture, Platform], mangled_name:str, options=False):
5083
"""
5184
``demangle_ms`` demangles a mangled Microsoft Visual Studio C++ name to a Type object.
@@ -75,7 +108,7 @@ def demangle_ms(archOrPlatform:Union[Architecture, Platform], mangled_name:str,
75108

76109
if (
77110
isinstance(options, binaryview.BinaryView) and demangleWithOptions(
78-
archOrPlatform.handle, mangled_name, ctypes.byref(handle), ctypes.byref(outName), ctypes.byref(outSize), options
111+
archOrPlatform.handle, mangled_name, ctypes.byref(handle), ctypes.byref(outName), ctypes.byref(outSize), options.handle
79112
)
80113
) or (
81114
isinstance(options, bool) and demangle(
@@ -112,7 +145,7 @@ def demangle_gnu3(arch, mangled_name, options=None):
112145
names = []
113146
if (
114147
isinstance(options, binaryview.BinaryView) and core.BNDemangleGNU3WithOptions(
115-
arch.handle, mangled_name, ctypes.byref(handle), ctypes.byref(outName), ctypes.byref(outSize), options
148+
arch.handle, mangled_name, ctypes.byref(handle), ctypes.byref(outName), ctypes.byref(outSize), options.handle
116149
)
117150
) or (
118151
isinstance(options, bool) and core.BNDemangleGNU3(

python/examples/export_svg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ def render_svg(function, offset, mode, form, showOpcodes, showAddresses, orignam
258258
.BaseStructureNameToken, .BaseStructureSeparatorToken {{
259259
fill: {rgbStr('BaseStructureNameColor')};
260260
}}
261-
.TextToken, .BeginMemoryOperandToken, .EndMemoryOperandToken {{
261+
.TextToken, .BeginMemoryOperandToken, .EndMemoryOperandToken, .AddressSeparatorToken {{
262262
fill: {rgbStr('TextToken')};
263263
}}
264264
</style>

0 commit comments

Comments
 (0)