commit 9107a2797c29c9c9c69914d173c0ec6f90538f67 Author: Mario Hüttel Date: Tue Aug 16 21:42:23 2022 +0200 Start first draft diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..95afc6e --- /dev/null +++ b/.gitignore @@ -0,0 +1,75 @@ +# Created by https://www.toptal.com/developers/gitignore/api/cmake,c +# Edit at https://www.toptal.com/developers/gitignore?templates=cmake,c + +### C ### +# Prerequisites +*.d + +# Object files +*.o +*.ko +*.obj +*.elf + +# Linker output +*.ilk +*.map +*.exp + +# Precompiled Headers +*.gch +*.pch + +# Libraries +*.lib +*.a +*.la +*.lo + +# Shared objects (inc. Windows DLLs) +*.dll +*.so +*.so.* +*.dylib + +# Executables +*.exe +*.out +*.app +*.i*86 +*.x86_64 +*.hex + +# Debug files +*.dSYM/ +*.su +*.idb +*.pdb + +# Kernel Module Compile Results +*.mod* +*.cmd +.tmp_versions/ +modules.order +Module.symvers +Mkfile.old +dkms.conf + +### CMake ### +CMakeLists.txt.user +CMakeCache.txt +CMakeFiles +CMakeScripts +Testing +Makefile +cmake_install.cmake +install_manifest.txt +compile_commands.json +CTestTestfile.cmake +_deps + +### CMake Patch ### +# External projects +*-prefix/ + +# End of https://www.toptal.com/developers/gitignore/api/cmake,c diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..299576d --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,19 @@ +cmake_minimum_required(VERSION 3.5) + +project(patchelfcrc LANGUAGES C) + +find_package(PkgConfig REQUIRED) +pkg_check_modules(ELF REQUIRED libelf) + +set (CFILES + main.c +) + +add_executable(${PROJECT_NAME} ${CFILES}) +target_link_libraries(${PROJECT_NAME} ${ELF_LIBRARIES}) +target_link_directories(${PROJECT_NAME} PRIVATE ${ELF_LIBRARY_DIRS}) +target_include_directories(${PROJECT_NAME} PRIVATE ${ELF_INCLUDE_DIRS}) + + + + diff --git a/main.c b/main.c new file mode 100644 index 0000000..6a83808 --- /dev/null +++ b/main.c @@ -0,0 +1,73 @@ +#include +#include +#include +#include + +#define print_err(fmt, ...) fprintf(stderr, (fmt), ## __VA_ARGS__); +#define print_debug(fmt, ...) do { \ + if (verbose) { \ + printf("[DBG] "fmt, ## __VA_ARGS__); \ + } \ + } while (0) + +enum granularity { + GRANULARITY_BYTE = 1, + GRANULARITY_16BIT, + GRANULARITY_32BIT, +}; + +struct command_line_options { + bool little_endian; + enum granularity granularity; +}; + +static error_t parse_opt(int key, char *arg, struct argp_state *state) +{ + struct command_line_options *args = (struct command_line_options *)state->input; + switch (key) { + case 'l': + args->little_endian = true; + break; + default: + return ARGP_ERR_UNKNOWN; + } +} + +static int parse_cmdline_options(int *argc, char ***argv, struct command_line_options *cmd_opts) +{ + if (!argc || !argv) + return -1000; + + static struct argp_option options[] = { + {"little-endian", 'l', 0, 0, "Memory image is little endian. Only relevant if granularity is greater than a single byte"}, + /* Sentinel */ + {NULL, 0, 0, 0, NULL} + }; + + static struct argp arg_parser = { + options, + parse_opt, + NULL, + NULL, + 0, 0, 0 + }; + + argp_parse(&arg_parser, *argc, *argv, 0, 0, cmd_opts); + +} + +int main(int argc, char **argv) +{ + bool verbose = true; + struct command_line_options cmd_opts; + + cmd_opts.little_endian = false; + cmd_opts.granularity = GRANULARITY_BYTE; + + parse_cmdline_options(&argc, &argv, &cmd_opts); + + print_debug("Start CRC patching\n"); + print_debug("Endianess: %s endian\n", (cmd_opts.little_endian ? "little" : "big")); + + return 0; +}