Skip to content

Commit

Permalink
add the ability to print functions (#80)
Browse files Browse the repository at this point in the history
* add the ability to print functions

* Seems to be a windows 2022 server build error. I can't reproduce on my devbox. Downgrade from windows-latest to windows-2019 for now.

Co-authored-by: Farzon Lotfi <[email protected]>
  • Loading branch information
farzonl and farzonl authored Aug 15, 2022
1 parent b9b08f8 commit 984d91e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 7 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/cmake.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
os: [ubuntu-latest, macos-latest, windows-2019]
steps:
- uses: actions/checkout@v2
- run: |
Expand All @@ -28,7 +28,7 @@ jobs:
- run: brew install capstone nasm mingw-w64 glfw glm
if: matrix.os == 'macOS-latest'
- run: choco install python3 nasm
if: matrix.os == 'windows-latest'
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
Expand All @@ -48,7 +48,7 @@ jobs:
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE

- name: Configure CMake (Linux\Windows)
if: matrix.os == 'ubuntu-latest' || matrix.os == 'windows-latest'
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
Expand All @@ -73,7 +73,7 @@ jobs:
run: ./src/test/Disassembler_TEST

- name: Test Windows
if: matrix.os == 'windows-latest'
if: matrix.os == 'windows-2019'
working-directory: ${{github.workspace}}/build
shell: bash
# Execute tests defined by the CMake configuration.
Expand Down
6 changes: 6 additions & 0 deletions int-tests/mac/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
# Project "mac-tests"
project(mac-tests ASM_NASM)
set(CMAKE_ASM_NASM_LINK_FLAGS "-macosx_version_min 10.13 -lSystem")
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin"
AND CMAKE_HOST_SYSTEM_VERSION VERSION_GREATER_EQUAL 20
AND CMAKE_HOST_SYSTEM_VERSION VERSION_LESS 21)
set(CMAKE_ASM_NASM_LINK_FLAGS "${CMAKE_ASM_NASM_LINK_FLAGS} -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib")
message(STATUS "Running on Big Sur")
endif ()
# Specify ASM linker
set(CMAKE_ASM_NASM_LINK_EXECUTABLE "ld <CMAKE_ASM_NASM_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")

Expand Down
5 changes: 4 additions & 1 deletion src/cli/binaryfilecli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ BinaryFileCLI::BinaryFileCLI(int argc, char **argv) : BaseCLI(argc, argv) {
// its only in BaseCLI
this->filename = parser.get<std::string>("f");
this->dynamicLibs = parser.get<std::string>("d");
this->shouldPrintFileNames = parser.get<bool>("F");
}

BinaryFileCLI::~BinaryFileCLI() {}
Expand All @@ -28,9 +29,11 @@ void BinaryFileCLI::configure_parser() {
parser.set_required<bool>(name, altName, description);
parser.set_optional<std::string>("d", "dynamic", "",
"input to the dynamic lib we want to load.");
parser.set_optional<bool>("F", "function", 0, "print out function names.");
}

void BinaryFileCLI::executeAction() {
this->BaseCLI::executeAction();
BinaryDisassemble::action(this->filename, this->dynamicLibs);
BinaryDisassemble::action(this->filename, this->dynamicLibs,
this->shouldPrintFileNames);
}
1 change: 1 addition & 0 deletions src/cli/binaryfilecli.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class BinaryFileCLI : public BaseCLI {
private:
std::string filename;
std::string dynamicLibs;
bool shouldPrintFileNames;
};

#endif // __binary_file_cli_h__
8 changes: 6 additions & 2 deletions src/runtime/binaryDisassemble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,16 @@ BinaryDisassemble::disassemble(const std::string &filename,

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

auto disasm = disassemble(filename, dynamicLibPaths);

out << *(binary.get()) << std::endl;
out << *(disasm.get()) << std::endl;
if (shouldPrintFileNames) {
out << binary->functionNames() << std::endl;
} else {
out << *(disasm.get()) << std::endl;
}
return true;
}
1 change: 1 addition & 0 deletions src/runtime/binaryDisassemble.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ class BinaryDisassemble {
disassemble(const std::string &filename, const std::string &dynamicLibPaths);
static bool action(const std::string &filename,
const std::string &dynamicLibPaths,
bool shouldPrintFileNames = false,
std::ostream &out = std::cout);
};

Expand Down
21 changes: 21 additions & 0 deletions src/runtime/internalData.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct BinaryTypes {
struct BinaryInternal {
BinaryInternal() {}
BinaryTypes binary;
std::vector<std::string> funcNames;
void setElf(std::unique_ptr<LIEF::ELF::Binary> &elf) {
binary.elf = std::move(elf);
}
Expand All @@ -34,6 +35,26 @@ struct BinaryInternal {
void setMachO(LIEF::MachO::Binary *machO) {
binary.machO = std::unique_ptr<LIEF::MachO::Binary>(machO);
}
LIEF::Binary::functions_t functions() {
if (binary.elf) {
return binary.elf->functions();
}
if (binary.pe) {
return binary.pe->functions();
}
if (binary.machO) {
return binary.machO->functions();
}
}
std::vector<std::string> &functionNames() {
auto funcs = functions();
if (funcNames.empty()) {
auto getFuncName = [](LIEF::Function &func) { return func.name(); };
std::transform(funcs.begin(), funcs.end(), std::back_inserter(funcNames),
getFuncName);
}
return funcNames;
}
};

#endif //__internal_data_h__
4 changes: 4 additions & 0 deletions src/runtime/runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,8 @@ Binary::Binary(std::string path) : mPath(path) {
mBinaryInternal = std::make_unique<BinaryInternal>();
}

std::vector<std::string> &Binary::functionNames() {
return mBinaryInternal->functionNames();
}

Binary::~Binary() {}
1 change: 1 addition & 0 deletions src/runtime/runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Binary {
OStype OS() const { return os; }

const std::vector<uint8_t> &Instructions() const;
std::vector<std::string> &functionNames();
friend class ASMParser;
};

Expand Down

0 comments on commit 984d91e

Please sign in to comment.