Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow building against latest Binary Ninja API version #134

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,12 @@ mv third_party/idasdk/idasdk_pro82/* third_party/idasdk
rmdir third_party/idasdk/idasdk_pro82
```

#### Binary Ninja

If building the Binary Ninja plugin, set `-DBINEXPORT_BINARYNINJA_CHANNEL` to either `stable` or `dev` to build against pinned versions of those channels.

`-DBINEXPORT_BINARYNINJA_LATEST=ON` can be used to instead build against the latest commit from [GitHub](https://github.com/Vector35/binaryninja-api).

#### Build BinExport

With all prerequisites in place, configure and build BinExport and run
Expand Down
2 changes: 2 additions & 0 deletions binaryninja/stubs/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Don't track as this will be updated on every use
binaryninjacore_latest.cc
2,600 changes: 2,430 additions & 170 deletions binaryninja/stubs/binaryninjacore.cc

Large diffs are not rendered by default.

2,435 changes: 2,321 additions & 114 deletions binaryninja/stubs/binaryninjacore_stable.cc

Large diffs are not rendered by default.

103 changes: 103 additions & 0 deletions binaryninja/stubs/regenerate-api-stubs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<#
Copyright 2011-2024 Google LLC

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

https://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.
#>

# Updates the Binary Ninja API stub file

# Exit on error
$ErrorActionPreference = "Stop"

# Function to get the canonical path
function Get-CanonicalPath {
param (
[string]$file
)
$file = Resolve-Path $file -ErrorAction Stop
$limit = 0
while ((Test-Path -PathType Leaf $file) -and ($limit -lt 1000)) {
$file = (Get-Item $file).Target
$limit++
}
return (Get-Item $file).FullName
}

$THIS = Split-Path $PSCommandPath -leaf

if (-not $args[0]) {
Write-Host "usage: $THIS PATH_TO_BINARYNINJA_API [stable]"
Write-Host "set 'stable' to update the stable stubs"
exit 1
}

$SUFFIX = switch ($args[1]) {
"stable" { "_stable" }
"latest" { "_latest" }
default { "" }
}

$BINARYNINJA_API_SRC = Get-CanonicalPath $args[0]

Set-Location $PSScriptRoot

$header = @"
// Copyright 2011-2024 Google LLC
//
// 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
//
// https://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.

// This file is auto-generated. To regenerate, run the "$THIS" script.

#include <cstdint>
#include <cstddef>

// clang-format off
#include "exceptions.h" // NOLINT
#define BINARYNINJACORE_LIBRARY
#include "binaryninjacore.h" // NOLINT
// clang-format on

extern "C" {
"@

$footer = @"
} // extern "C"
"@

# The following extracts all C API functions from the Binary Ninja API
# and creates empty stub functions for them. This is later compiled
# into an API compatible library that is used to link against. This
# avoids having to use install a copy of Binary Ninja on the machine
# that builds BinExport.
# Note that the script line below can be replaced with a small Clang
# tool to make it more robust.
$functions = Get-Content "${BINARYNINJA_API_SRC}/binaryninjacore.h" |
foreach {$_ -replace "\s+// .*", "" } |
clang-format --style='{BasedOnStyle: Google, Language: Cpp, ColumnLimit: 100000}' |
Select-String '^BINARYNINJACOREAPI' |
ForEach-Object {
$_ -replace '(BINARYNINJACOREAPI void .*)\;$', '$1 {}' -replace '(BINARYNINJACOREAPI .*)\;$', '$1 { return {}; }'
}

$functions = ,$header + $functions + ,$footer
$functions | Out-String | % Trim | Set-Content -Path "binaryninjacore${SUFFIX}.cc"
13 changes: 11 additions & 2 deletions binaryninja/stubs/regenerate-api-stubs.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ THIS=$(basename "$0")
THIS_DIR=$(dirname "$(canonical_path "$0")")

if [ -z "$1" ]; then
echo "usage: ${THIS} PATH_TO_BINARYNINJA_API" 2>&1
echo "usage: ${THIS} PATH_TO_BINARYNINJA_API [stable]" 2>&1
echo "set 'stable' to update the stable stubs" 2>&1
exit 1
fi

if [ "$2" = "stable" ]; then
SUFFIX="_stable"
elif [ "$2" = "latest" ]; then
SUFFIX="_latest"
else
SUFFIX=""
fi

BINARYNINJA_API_SRC=$(canonical_path "$1")

cd "${THIS_DIR}"
Expand Down Expand Up @@ -98,4 +107,4 @@ cat <<EOF
} // extern "C"
EOF
) | \
clang-format --style=Google > binaryninjacore.cc
clang-format --style=Google > "binaryninjacore${SUFFIX}.cc"
35 changes: 27 additions & 8 deletions cmake/BinExportDeps.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -93,14 +93,19 @@ find_package(Protobuf 3.14 REQUIRED) # Make protobuf_generate_cpp available

# Binary Ninja API
if(BINEXPORT_ENABLE_BINARYNINJA)
if(BINEXPORT_BINARYNINJA_CHANNEL STREQUAL "stable")
set(_binexport_binaryninjacore_suffix "_stable")
set(_binexport_binaryninja_git_tag
"59e569906828e91e4884670c2bba448702f5a31d") # 2023-09-19 v3.5.4526
if(BINEXPORT_BINARYNINJA_LATEST)
set(_binexport_binaryninjacore_suffix "_latest")
set(_binexport_binaryninja_git_tag "dev")
else()
set(_binexport_binaryninjacore_suffix "")
set(_binexport_binaryninja_git_tag
"6e2b374dece03f6fb48a1615fa2bfee809ec2157") # 2023-09-24
if(BINEXPORT_BINARYNINJA_CHANNEL STREQUAL "stable")
set(_binexport_binaryninjacore_suffix "_stable")
set(_binexport_binaryninja_git_tag
"967ffc9bd705f6820c4dbfe97f301d52facb1417") # 2024-08-14 v4.1.5902
else()
set(_binexport_binaryninjacore_suffix "")
set(_binexport_binaryninja_git_tag
"de23abd02ab27b6623656ed04e9b62d4dc1361b7") # 2024-08-24
endif()
endif()
FetchContent_Declare(binaryninjaapi
GIT_REPOSITORY https://github.com/Vector35/binaryninja-api.git
Expand All @@ -110,8 +115,22 @@ if(BINEXPORT_ENABLE_BINARYNINJA)
if(NOT binaryninjaapi_POPULATED)
FetchContent_Populate(binaryninjaapi) # For binaryninjaapi_SOURCE_DIR
endif()
if(BINEXPORT_BINARYNINJA_LATEST)
if(WIN32)
find_program(POWERSHELL_PATH NAMES powershell pwsh)
set(_stub_command "${POWERSHELL_PATH} ${BINEXPORT_SOURCE_DIR}/binaryninja/stubs/regenerate-api-stubs.ps1")
else()
set(_stub_command "${BINEXPORT_SOURCE_DIR}/binaryninja/stubs/regenerate-api-stubs.sh")
endif()
add_custom_command(
OUTPUT ${BINEXPORT_SOURCE_DIR}/binaryninja/stubs/binaryninjacore${_binexport_binaryninjacore_suffix}.cc
COMMAND ${_stub_command} ${binaryninjaapi_SOURCE_DIR} latest
DEPENDS ${binaryninjaapi_SOURCE_DIR}/binaryninjacore.h
COMMENT "Updating stubs"
)
endif()
add_library(binaryninjacore SHARED
binaryninja/stubs/binaryninjacore${_binexport_binaryninjacore_suffix}.cc
${BINEXPORT_SOURCE_DIR}/binaryninja/stubs/binaryninjacore${_binexport_binaryninjacore_suffix}.cc
)
set_target_properties(binaryninjacore PROPERTIES
SOVERSION 1
Expand Down
2 changes: 2 additions & 0 deletions cmake/BinExportOptions.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,5 @@ option(BINEXPORT_ENABLE_BINARYNINJA "Build the Binary Ninja plugin" ON)

set(BINEXPORT_BINARYNINJA_CHANNEL "stable" CACHE
STRING "Binary Ninja channel, either 'stable' or 'dev'")

option(BINEXPORT_BINARYNINJA_LATEST "Use the latest Binary Ninja API from GitHub" OFF)