Skip to content

Commit

Permalink
upgrade to the latest lief version (#86)
Browse files Browse the repository at this point in the history
* upgrade to the latest lief version

* fix formatting

* LIEF dropped MT postfix for windows, we will need to test the debug build to see if that also needs to change

* update version.txt to 0.0.2

* add caching to speed up windows debug dependency builds

Co-authored-by: Farzon Lotfi <[email protected]>
  • Loading branch information
farzonl and farzonl committed Aug 21, 2022
1 parent 3713d67 commit 96953b0
Show file tree
Hide file tree
Showing 9 changed files with 239 additions and 23 deletions.
133 changes: 133 additions & 0 deletions .github/workflows/cmake-debug.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
name: CMake Debug

on:
push:
branches:
- master
pull_request:
branches:
- '**'

env:
# Customize the CMake build type here (Release, Debug, RelWithDebInfo, etc.)
BUILD_TYPE: Debug

jobs:
build:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-2019]
include:
- os: ubuntu-latest
artifact_dlib_ext: .so
artifact_staticlib_ext: .a
- os: windows-2019
artifact_exec_ext: .exe
artifact_dlib_ext: .dll
artifact_staticlib_ext: .lib
# Note: I wanted to use env.BUILD_TYPE, but it isn't taking
#artifact_out_dir: ${{ BUILD_TYPE }}/
artifact_out_dir: Debug/
artifact_os_name: Windows
artifact_arch: x86_64
- os: macos-latest
artifact_dlib_ext: .dylib
artifact_staticlib_ext: .a
steps:
- uses: actions/checkout@v2
- run: |
sudo apt install libcapstone-dev nasm mingw-w64 libedit-dev \
libgl1-mesa-dev mesa-utils libgl1-mesa-glx libxrandr-dev \
libxinerama-dev libxcursor-dev libxi-dev
if: matrix.os == 'ubuntu-latest'
- run: brew install capstone nasm mingw-w64 glfw glm
if: matrix.os == 'macOS-latest'
- run: choco install python3 nasm
if: matrix.os == 'windows-2019'
- name: Create Build Environment
# Some projects don't allow in-source building, so create a separate build directory
# We'll use this as our working directory for all subsequent commands
run: cmake -E make_directory ${{github.workspace}}/build
- name: Cache C++ dependencies in Packages Directory
uses: actions/cache@v3
with:
path: |
${{github.workspace}}/packages
key: ${{ runner.OS }}-c++-packages-cache-${{ hashFiles('${{github.workspace}}/depsCache.json') }}
restore-keys: |
${{ runner.OS }}-c++-packages-cache
#NOTE: this is temporary until i understand a bit more why i can't build binutil
# Monitored with issue-32 https://github.com/farzonl/Disassembler/issues/32
- name: Configure CMake (Mac)
if: matrix.os == 'macOS-latest'
# Use a bash shell so we can use the same syntax for environment variable
# access regardless of the host operating system
shell: bash
working-directory: ${{github.workspace}}/build
# Note the current convention is to use the -S and -B options here to specify source
# and build directories, but this is only available with CMake 3.13 and higher.
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE

- name: Configure CMake (Linux\Windows)
if: matrix.os == 'ubuntu-latest' || matrix.os == 'windows-2019'
# Use a bash shell so we can use the same syntax for environment variable
# access regardless of the host operating system
shell: bash
working-directory: ${{github.workspace}}/build
# Note the current convention is to use the -S and -B options here to specify source
# and build directories, but this is only available with CMake 3.13 and higher.
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE -DBUILD_INT_TESTS=true

- name: Build
working-directory: ${{github.workspace}}/build
shell: bash
# Execute the build. You can specify a specific target with "--target <NAME>"
run: cmake --build . --config $BUILD_TYPE

- name: Test (Unix-like)
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macOS-latest'
working-directory: ${{github.workspace}}/build
shell: bash
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ./src/test/Disassembler_TEST

- name: Test Windows
if: matrix.os == 'windows-2019'
working-directory: ${{github.workspace}}/build
shell: bash
# Execute tests defined by the CMake configuration.
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ./src/test/$BUILD_TYPE/Disassembler_TEST.exe
- name: Prepare Binaries for upload (Mac\Linux)
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macOS-latest'
shell: bash
run: |
mkdir ${{github.workspace}}/artifacts
cp build/src/libDisassembler${{ matrix.artifact_staticlib_ext }} ${{github.workspace}}/artifacts
cp build/src/cli/disasm ${{github.workspace}}/artifacts
cp build/src/gui/disasm-gui ${{github.workspace}}/artifacts
cp build/plugins/xedPlugin/libxedPlugin${{ matrix.artifact_dlib_ext }} ${{github.workspace}}/artifacts
pushd ${{github.workspace}}
zip -r Gozihr-$(uname -s)-$(uname -m).zip artifacts
popd
- name: Prepare Binaries for upload (windows)
if: matrix.os == 'windows-2019'
shell: powershell
run: |
[system.io.directory]::CreateDirectory("${{github.workspace}}/artifacts")
Copy-Item "build/src/${{ matrix.artifact_out_dir}}Disassembler${{ matrix.artifact_staticlib_ext }}" -Destination "${{github.workspace}}/artifacts"
Copy-Item "build/src/cli/${{ matrix.artifact_out_dir }}disasm${{ matrix.artifact_exec_ext }}" -Destination "${{github.workspace}}/artifacts"
Copy-Item "build/src/gui/${{ matrix.artifact_out_dir }}disasm-gui${{ matrix.artifact_exec_ext }}" -Destination "${{github.workspace}}/artifacts"
Copy-Item "build/plugins/xedPlugin/${{ matrix.artifact_out_dir }}xedPlugin${{ matrix.artifact_dlib_ext }}" -Destination "${{github.workspace}}/artifacts"
Compress-Archive -Path ${{github.workspace}}/artifacts/* -DestinationPath Gozihr-${{matrix.artifact_os_name}}-${{matrix.artifact_arch}}.zip
- name: 'Upload Pull Request Artifact'
uses: actions/upload-artifact@v3
if: startsWith(github.ref, 'refs/pull/')
with:
name: Gozihr Pull Request Artifacts
path: Gozihr-*.zip
retention-days: 5
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: CMake
name: CMake Release

on:
push:
Expand Down Expand Up @@ -50,6 +50,15 @@ jobs:
# We'll use this as our working directory for all subsequent commands
run: cmake -E make_directory ${{github.workspace}}/build

- name: Cache C++ dependencies in Packages Directory
uses: actions/cache@v3
with:
path: |
${{github.workspace}}/packages
key: ${{ runner.OS }}-c++-packages-cache-${{ hashFiles('${{github.workspace}}/depsCache.json') }}
restore-keys: |
${{ runner.OS }}-c++-packages-cache
#NOTE: this is temporary until i understand a bit more why i can't build binutil
# Monitored with issue-32 https://github.com/farzonl/Disassembler/issues/32
- name: Configure CMake (Mac)
Expand All @@ -62,7 +71,14 @@ jobs:
# and build directories, but this is only available with CMake 3.13 and higher.
# The CMake binaries on the Github Actions machines are (as of this writing) 3.12
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE

- name: Cache C++ dependencies in Packages Directory
uses: actions/cache@v3
with:
path: |
${{github.workspace}}/packages
key: ${{ runner.OS }}-c++-packages-cache-${{ hashFiles('${{github.workspace}}/depsCache.json') }}
restore-keys: |
${{ runner.OS }}-c++-packages-cache
- name: Configure CMake (Linux\Windows)
if: matrix.os == 'ubuntu-latest' || matrix.os == 'windows-2019'
# Use a bash shell so we can use the same syntax for environment variable
Expand Down Expand Up @@ -134,8 +150,10 @@ jobs:
- name: Set variables (Windows)
if: matrix.os == 'windows-2019'
run: |
echo "VERSION=0.0.1" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
echo "APPNAME=Gozihr" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
$APP = type .\src\version\AppName.txt
$VER = type .\src\version\Version.txt
echo "VERSION=$VER" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
echo "APPNAME=$APP" | Out-File -FilePath $Env:GITHUB_ENV -Encoding utf8 -Append
- name: Upload binaries to Release
uses: softprops/action-gh-release@v1
Expand Down
10 changes: 5 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ if(WIN32)
endif()
endif()

set(LIEF_VERSION "0.11.3")
set(LIEF_VERSION "0.12.1")
if( DEFINED BUILDLIEFSRC OR (WIN32 AND CMAKE_BUILD_TYPE MATCHES Debug))
message(STATUS "starting a LIEF source build")
download_file(https://github.com/lief-project/LIEF/archive/${LIEF_VERSION}.zip
${CMAKE_SOURCE_DIR}/packages/LIEF-${LIEF_VERSION}.zip
MD5
fc6f9e277de0f25993007c0b10de2daf
465563d8342e148a3d82b6b1cb36bd11
)
decompress(${CMAKE_SOURCE_DIR}/packages/LIEF-${LIEF_VERSION}.zip ${CMAKE_SOURCE_DIR}/packages/LIEF-src)
buildLIEF(${CMAKE_SOURCE_DIR}/packages/LIEF-src
Expand All @@ -89,19 +89,19 @@ else()
download_file(https://github.com/lief-project/LIEF/releases/download/${LIEF_VERSION}/LIEF-${LIEF_VERSION}-Linux-x86_64.tar.gz
${CMAKE_SOURCE_DIR}/packages/LIEF-${LIEF_VERSION}.tar.gz
MD5
cffdd77556bd35e8cc32ff9742605a4e)
eb604e6d494e86a888e41ef4df549b3a)
decompress(${CMAKE_SOURCE_DIR}/packages/LIEF-${LIEF_VERSION}.tar.gz ${CMAKE_SOURCE_DIR}/packages/LIEF)
elseif(WIN32)
download_file(https://github.com/lief-project/LIEF/releases/download/${LIEF_VERSION}/LIEF-${LIEF_VERSION}-win64.zip
${CMAKE_SOURCE_DIR}/packages/LIEF-${LIEF_VERSION}-win64.zip
MD5
a5e6a5ef5933b1296739d859b198f857)
ca4665c47fb6d4b68a30199195fa7037)
decompress(${CMAKE_SOURCE_DIR}/packages/LIEF-${LIEF_VERSION}-win64.zip ${CMAKE_SOURCE_DIR}/packages/LIEF)
elseif(APPLE)
download_file(https://github.com/lief-project/LIEF/releases/download/${LIEF_VERSION}/LIEF-${LIEF_VERSION}-Darwin-x86_64.tar.gz
${CMAKE_SOURCE_DIR}/packages/LIEF-${LIEF_VERSION}.tar.gz
MD5
d033f69581f61386cca64ff0fb9b4e88)
5d609361f22b6f9a8dc22f2c6fb541a7)
decompress(${CMAKE_SOURCE_DIR}/packages/LIEF-${LIEF_VERSION}.tar.gz ${CMAKE_SOURCE_DIR}/packages/LIEF)
else()
message(FATAL_ERROR "No other platforms are supported.")
Expand Down
35 changes: 35 additions & 0 deletions depsCache.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"LIEF" : {
"Src": "465563d8342e148a3d82b6b1cb36bd11",
"Linux": "eb604e6d494e86a888e41ef4df549b3a",
"Windows" : "ca4665c47fb6d4b68a30199195fa7037",
"MacOS": "5d609361f22b6f9a8dc22f2c6fb541a7"
},
"Catch2" : {
"Src": "209cbb2f8f37224cc5f7261cf9388bc2"
},
"CMDParser" : {
"Src": "8449f7da1097659698193bc0ddb24ef7"
},
"nlohmannJson" : {
"Src": "7804b38146921d03374549c9e2a5e3acda097814c43caf2b96a0278e58df26e0"
},
"Capstone" : {
"Windows": "a6ad5a3bd6842cb7fadc3f3e5ed8bf20"
},
"myers-diff" : {
"Src" : "90486b3453ab76ab2a1498dc64c79060"
},
"glad" : {
"Src" : "262e8984dbde2f359207ffa8e609aa50"
},
"glfw3" : {
"Src": "f794d9ad899a64894782884be79d644b"
},
"imgui" : {
"Src": "78af0cf170fb3f2a431eb39cac7f55b8"
},
"ImGuiFileDialog": {
"Src" : "13e1abe33aeb0b869307a238967038ae"
}
}
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ elseif(WIN32)
set(CAPSTONE_INCLUDE_DIRS ${CAPSTONE_DIR}/include/capstone)

set(CAPSTONE_LDFLAGS "${CAPSTONE_DIR}/${CMAKE_STATIC_LIBRARY_PREFIX}capstone${CMAKE_STATIC_LIBRARY_SUFFIX}")
set(LIEF_LDFLAGS "${LIEF_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}LIEF${MTPOSTFIX}${CMAKE_STATIC_LIBRARY_SUFFIX}")
set(LIEF_LDFLAGS "${LIEF_DIR}/lib/${CMAKE_STATIC_LIBRARY_PREFIX}LIEF${CMAKE_STATIC_LIBRARY_SUFFIX}")
list(APPEND TargetLibraries
${LIEF_LDFLAGS}
)
Expand Down
42 changes: 31 additions & 11 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,43 +75,63 @@ void Binary::elfParser() {
std::unique_ptr<LIEF::ELF::Binary> elfBinary =
LIEF::ELF::Parser::parse(this->Path());
auto header = elfBinary->header();
LIEF::ELF::Section &textSection = elfBinary->get_section(".text");
textSection.content().swap(this->mInstructions);
LIEF::ELF::Section *textSection = elfBinary->get_section(".text");
this->mInstructions.resize(
std::max(this->mInstructions.size(), textSection->content().size()));
std::copy(textSection->content().begin(), textSection->content().end(),
this->mInstructions.begin());

this->os = OStype::LINUX;
this->arch = ::GetArch(header.machine_type());
this->textSectionStartAddress = textSection.virtual_address();
this->textSectionStartAddress = textSection->virtual_address();
this->mBinaryInternal->setElf(elfBinary);
}

void Binary::peParser() {

auto peBinary = LIEF::PE::Parser::parse(this->Path());
LIEF::PE::Section &textSection = peBinary->get_section(".text");
textSection.content().swap(this->mInstructions);
LIEF::PE::Section *textSection = peBinary->get_section(".text");
this->mInstructions.resize(
std::max(this->mInstructions.size(), textSection->content().size()));
std::copy(textSection->content().begin(), textSection->content().end(),
this->mInstructions.begin());
this->os = OStype::WINDOWS;
this->arch = ::GetArch(peBinary->header().machine());
this->textSectionStartAddress = textSection.virtual_address();
this->textSectionStartAddress = textSection->virtual_address();
this->mBinaryInternal->setPE(peBinary);
}

void Binary::machOParser() {
// For fat binary we take the last one...
LIEF::MachO::FatBinary *fat =
LIEF::MachO::Parser::parse(this->Path()).release();
LIEF::MachO::Binary *binaryData = nullptr;
std::unique_ptr<LIEF::MachO::Binary> binaryData = nullptr;
if (fat) {
if (fat->size() > 1) {
std::cout << "Warning number of mach-O binary files is: " << fat->size()
<< std::endl;
for (int i = 0; i < fat->size(); i++) {
std::cout << "fat[" << i
<< "] Arch = " << ::GetArch(fat->at(i)->header().cpu_type())
<< std::endl;
}
std::cout << "Picking the last format: "
<< GetArch(fat->at(fat->size() - 1)->header().cpu_type())
<< std::endl;
}
binaryData = fat->pop_back();
delete fat;
}

auto header = binaryData->header();
LIEF::MachO::Section &textSection = binaryData->get_section("__text");
textSection.content().swap(this->mInstructions);
LIEF::MachO::Section *textSection = binaryData->get_section("__text");
this->mInstructions.resize(
std::max(this->mInstructions.size(), textSection->content().size()));
std::copy(textSection->content().begin(), textSection->content().end(),
this->mInstructions.begin());

this->os = OStype::MACOS;
this->arch = ::GetArch(header.cpu_type());
this->textSectionStartAddress = textSection.virtual_address();
this->textSectionStartAddress = textSection->virtual_address();
this->mBinaryInternal->setMachO(binaryData);
}

Expand Down
10 changes: 9 additions & 1 deletion src/runtime/binaryDisassemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,20 @@ BinaryDisassemble::disassemble(const std::string &filename,
return BinaryDisassemble::disassemble(*(binary.get()), dType);
}

std::unique_ptr<AbstractDisassembler>
BinaryDisassemble::disassemble(const Binary &binary,
const std::string &dynamicLibPaths) {
DisassemblerType dType =
Disassembler::checkAndInitDynamicDisassemblers(dynamicLibPaths);
return BinaryDisassemble::disassemble(binary, dType);
}

bool BinaryDisassemble::action(const std::string &filename,
const std::string &dynamicLibPaths,
bool shouldPrintFileNames, std::ostream &out) {
std::unique_ptr<Binary> binary = ASMParser::Parser(filename);

auto disasm = disassemble(filename, dynamicLibPaths);
auto disasm = disassemble(*(binary.get()), dynamicLibPaths);

out << *(binary.get()) << std::endl;
if (shouldPrintFileNames) {
Expand Down
2 changes: 2 additions & 0 deletions src/runtime/binaryDisassemble.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ class BinaryDisassemble {
DisassemblerType dtype = DisassemblerType::CAPSTONE);
static std::unique_ptr<AbstractDisassembler>
disassemble(const std::string &filename, const std::string &dynamicLibPaths);
static std::unique_ptr<AbstractDisassembler>
disassemble(const Binary &binary, const std::string &dynamicLibPaths);
static bool action(const std::string &filename,
const std::string &dynamicLibPaths,
bool shouldPrintFileNames = false,
Expand Down
2 changes: 1 addition & 1 deletion src/version/Version.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.0.1
0.0.2

0 comments on commit 96953b0

Please sign in to comment.