Add linked list for input sections

This commit is contained in:
Mario Hüttel 2022-08-20 11:26:04 +02:00
parent 9b803a887e
commit 9ee80cc8f7
4 changed files with 40 additions and 2 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "3rdparty/libfort"] [submodule "3rdparty/libfort"]
path = 3rdparty/libfort path = 3rdparty/libfort
url = https://git.shimatta.de/3rd-party/libfort url = https://git.shimatta.de/3rd-party/libfort
[submodule "linklist-lib"]
path = linklist-lib
url = https://git.shimatta.de/mhu/linklist-lib.git

View File

@ -25,9 +25,10 @@ add_compile_options(-Wall -Wextra -Wold-style-declaration -Wuninitialized -Wmayb
set(FORT_ENABLE_TESTING OFF CACHE INTERNAL "") set(FORT_ENABLE_TESTING OFF CACHE INTERNAL "")
add_subdirectory(3rdparty/libfort) add_subdirectory(3rdparty/libfort)
add_subdirectory(linklist-lib)
add_executable(${PROJECT_NAME} ${CFILES}) add_executable(${PROJECT_NAME} ${CFILES})
target_link_libraries(${PROJECT_NAME} ${ELF_LIBRARIES} fort) target_link_libraries(${PROJECT_NAME} ${ELF_LIBRARIES} fort linklist-lib)
target_link_directories(${PROJECT_NAME} PRIVATE ${ELF_LIBRARY_DIRS}) target_link_directories(${PROJECT_NAME} PRIVATE ${ELF_LIBRARY_DIRS})
target_include_directories(${PROJECT_NAME} PRIVATE ${ELF_INCLUDE_DIRS}) target_include_directories(${PROJECT_NAME} PRIVATE ${ELF_INCLUDE_DIRS})
target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include") target_include_directories(${PROJECT_NAME} PRIVATE "${CMAKE_CURRENT_BINARY_DIR}/include")

1
linklist-lib Submodule

@ -0,0 +1 @@
Subproject commit 18b3ab377ac30516f977e9831813ea37189d4028

35
main.c
View File

@ -6,6 +6,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <patchelfcrc/named_crcs.h> #include <patchelfcrc/named_crcs.h>
#include <patchelfcrc/version.h> #include <patchelfcrc/version.h>
#include <linklist-lib/singly-linked-list.h>
#define print_err(fmt, ...) fprintf(stderr, (fmt), ## __VA_ARGS__); #define print_err(fmt, ...) fprintf(stderr, (fmt), ## __VA_ARGS__);
#define print_debug(fmt, ...) do { \ #define print_debug(fmt, ...) do { \
@ -44,6 +45,7 @@ struct command_line_options {
bool has_end_magic; bool has_end_magic;
uint32_t end_magic; uint32_t end_magic;
bool list; bool list;
SlList *section_list;
}; };
static error_t parse_opt(int key, char *arg, struct argp_state *state) static error_t parse_opt(int key, char *arg, struct argp_state *state)
@ -71,6 +73,10 @@ static error_t parse_opt(int key, char *arg, struct argp_state *state)
case 'v': case 'v':
args->verbose = true; args->verbose = true;
break; break;
case 'S':
/* Section */
args->section_list = sl_list_append(args->section_list, strdup(arg));
break;
case 'g': case 'g':
if (!strcmp(arg, "byte")) if (!strcmp(arg, "byte"))
args->granularity = GRANULARITY_BYTE; args->granularity = GRANULARITY_BYTE;
@ -141,11 +147,14 @@ static void prepare_default_opts(struct command_line_options *opts)
opts->has_end_magic = false; opts->has_end_magic = false;
opts->has_start_magic = false; opts->has_start_magic = false;
opts->list = false; opts->list = false;
opts->section_list = NULL;
} }
static void print_verbose_start_info(const struct command_line_options *cmd_opts) static void print_verbose_start_info(const struct command_line_options *cmd_opts)
{ {
bool verbose = cmd_opts->verbose; bool verbose = cmd_opts->verbose;
int i;
SlList *list_iter;
const struct named_crc *predef_crc; const struct named_crc *predef_crc;
print_debug("Start CRC patching\n"); print_debug("Start CRC patching\n");
@ -162,6 +171,27 @@ static void print_verbose_start_info(const struct command_line_options *cmd_opts
print_debug("Predefined CRC detected: %s\n", predef_crc->name); print_debug("Predefined CRC detected: %s\n", predef_crc->name);
} }
if (cmd_opts->section_list) {
for (list_iter = cmd_opts->section_list, i = 1; list_iter; list_iter = sl_list_next(list_iter), i++) {
print_debug("Input section [%d]: \"%s\"\n", i, (const char *)list_iter->data);
}
}
}
static void free_cmd_args(struct command_line_options *opts)
{
SlList *list_iter;
/* Free the output section names */
for (list_iter = opts->section_list; list_iter; list_iter = sl_list_next(list_iter)) {
if (list_iter->data)
free(list_iter->data);
}
/* Free the section list */
sl_list_free(opts->section_list);
opts->section_list = NULL;
} }
int main(int argc, char **argv) int main(int argc, char **argv)
@ -177,8 +207,11 @@ int main(int argc, char **argv)
if (cmd_opts.list) { if (cmd_opts.list) {
list_predefined_crcs(); list_predefined_crcs();
return 0; goto free_cmds;
} }
free_cmds:
free_cmd_args(&cmd_opts);
return 0; return 0;
} }