Skip to content

Commit 984d91e

Browse files
authored
add the ability to print functions (#80)
* 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 <hi@farzon.org>
1 parent b9b08f8 commit 984d91e

File tree

9 files changed

+48
-7
lines changed

9 files changed

+48
-7
lines changed

.github/workflows/cmake.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ jobs:
1717
runs-on: ${{ matrix.os }}
1818
strategy:
1919
matrix:
20-
os: [ubuntu-latest, macos-latest, windows-latest]
20+
os: [ubuntu-latest, macos-latest, windows-2019]
2121
steps:
2222
- uses: actions/checkout@v2
2323
- run: |
@@ -28,7 +28,7 @@ jobs:
2828
- run: brew install capstone nasm mingw-w64 glfw glm
2929
if: matrix.os == 'macOS-latest'
3030
- run: choco install python3 nasm
31-
if: matrix.os == 'windows-latest'
31+
if: matrix.os == 'windows-2019'
3232
- name: Create Build Environment
3333
# Some projects don't allow in-source building, so create a separate build directory
3434
# We'll use this as our working directory for all subsequent commands
@@ -48,7 +48,7 @@ jobs:
4848
run: cmake $GITHUB_WORKSPACE -DCMAKE_BUILD_TYPE=$BUILD_TYPE
4949

5050
- name: Configure CMake (Linux\Windows)
51-
if: matrix.os == 'ubuntu-latest' || matrix.os == 'windows-latest'
51+
if: matrix.os == 'ubuntu-latest' || matrix.os == 'windows-2019'
5252
# Use a bash shell so we can use the same syntax for environment variable
5353
# access regardless of the host operating system
5454
shell: bash
@@ -73,7 +73,7 @@ jobs:
7373
run: ./src/test/Disassembler_TEST
7474

7575
- name: Test Windows
76-
if: matrix.os == 'windows-latest'
76+
if: matrix.os == 'windows-2019'
7777
working-directory: ${{github.workspace}}/build
7878
shell: bash
7979
# Execute tests defined by the CMake configuration.

int-tests/mac/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
# Project "mac-tests"
22
project(mac-tests ASM_NASM)
33
set(CMAKE_ASM_NASM_LINK_FLAGS "-macosx_version_min 10.13 -lSystem")
4+
if (CMAKE_HOST_SYSTEM_NAME STREQUAL "Darwin"
5+
AND CMAKE_HOST_SYSTEM_VERSION VERSION_GREATER_EQUAL 20
6+
AND CMAKE_HOST_SYSTEM_VERSION VERSION_LESS 21)
7+
set(CMAKE_ASM_NASM_LINK_FLAGS "${CMAKE_ASM_NASM_LINK_FLAGS} -L /Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/lib")
8+
message(STATUS "Running on Big Sur")
9+
endif ()
410
# Specify ASM linker
511
set(CMAKE_ASM_NASM_LINK_EXECUTABLE "ld <CMAKE_ASM_NASM_LINK_FLAGS> <LINK_FLAGS> <OBJECTS> -o <TARGET> <LINK_LIBRARIES>")
612

src/cli/binaryfilecli.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ BinaryFileCLI::BinaryFileCLI(int argc, char **argv) : BaseCLI(argc, argv) {
1717
// its only in BaseCLI
1818
this->filename = parser.get<std::string>("f");
1919
this->dynamicLibs = parser.get<std::string>("d");
20+
this->shouldPrintFileNames = parser.get<bool>("F");
2021
}
2122

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

3335
void BinaryFileCLI::executeAction() {
3436
this->BaseCLI::executeAction();
35-
BinaryDisassemble::action(this->filename, this->dynamicLibs);
37+
BinaryDisassemble::action(this->filename, this->dynamicLibs,
38+
this->shouldPrintFileNames);
3639
}

src/cli/binaryfilecli.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ class BinaryFileCLI : public BaseCLI {
2626
private:
2727
std::string filename;
2828
std::string dynamicLibs;
29+
bool shouldPrintFileNames;
2930
};
3031

3132
#endif // __binary_file_cli_h__

src/runtime/binaryDisassemble.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ BinaryDisassemble::disassemble(const std::string &filename,
3535

3636
bool BinaryDisassemble::action(const std::string &filename,
3737
const std::string &dynamicLibPaths,
38-
std::ostream &out) {
38+
bool shouldPrintFileNames, std::ostream &out) {
3939
std::unique_ptr<Binary> binary = ASMParser::Parser(filename);
4040

4141
auto disasm = disassemble(filename, dynamicLibPaths);
4242

4343
out << *(binary.get()) << std::endl;
44-
out << *(disasm.get()) << std::endl;
44+
if (shouldPrintFileNames) {
45+
out << binary->functionNames() << std::endl;
46+
} else {
47+
out << *(disasm.get()) << std::endl;
48+
}
4549
return true;
4650
}

src/runtime/binaryDisassemble.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class BinaryDisassemble {
2727
disassemble(const std::string &filename, const std::string &dynamicLibPaths);
2828
static bool action(const std::string &filename,
2929
const std::string &dynamicLibPaths,
30+
bool shouldPrintFileNames = false,
3031
std::ostream &out = std::cout);
3132
};
3233

src/runtime/internalData.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct BinaryTypes {
2222
struct BinaryInternal {
2323
BinaryInternal() {}
2424
BinaryTypes binary;
25+
std::vector<std::string> funcNames;
2526
void setElf(std::unique_ptr<LIEF::ELF::Binary> &elf) {
2627
binary.elf = std::move(elf);
2728
}
@@ -34,6 +35,26 @@ struct BinaryInternal {
3435
void setMachO(LIEF::MachO::Binary *machO) {
3536
binary.machO = std::unique_ptr<LIEF::MachO::Binary>(machO);
3637
}
38+
LIEF::Binary::functions_t functions() {
39+
if (binary.elf) {
40+
return binary.elf->functions();
41+
}
42+
if (binary.pe) {
43+
return binary.pe->functions();
44+
}
45+
if (binary.machO) {
46+
return binary.machO->functions();
47+
}
48+
}
49+
std::vector<std::string> &functionNames() {
50+
auto funcs = functions();
51+
if (funcNames.empty()) {
52+
auto getFuncName = [](LIEF::Function &func) { return func.name(); };
53+
std::transform(funcs.begin(), funcs.end(), std::back_inserter(funcNames),
54+
getFuncName);
55+
}
56+
return funcNames;
57+
}
3758
};
3859

3960
#endif //__internal_data_h__

src/runtime/runtime.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,8 @@ Binary::Binary(std::string path) : mPath(path) {
1616
mBinaryInternal = std::make_unique<BinaryInternal>();
1717
}
1818

19+
std::vector<std::string> &Binary::functionNames() {
20+
return mBinaryInternal->functionNames();
21+
}
22+
1923
Binary::~Binary() {}

src/runtime/runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Binary {
4444
OStype OS() const { return os; }
4545

4646
const std::vector<uint8_t> &Instructions() const;
47+
std::vector<std::string> &functionNames();
4748
friend class ASMParser;
4849
};
4950

0 commit comments

Comments
 (0)