Start first draft

This commit is contained in:
Mario Hüttel 2022-08-16 21:42:23 +02:00
commit 9107a2797c
3 changed files with 167 additions and 0 deletions

75
.gitignore vendored Normal file
View File

@ -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

19
CMakeLists.txt Normal file
View File

@ -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})

73
main.c Normal file
View File

@ -0,0 +1,73 @@
#include <stdio.h>
#include <libelf.h>
#include <argp.h>
#include <stdbool.h>
#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;
}