From 1d5219cc1813326718e6416c464f29762695f94c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20H=C3=BCttel?= Date: Fri, 6 Jan 2023 16:25:59 +0100 Subject: [PATCH] Add print_warn() macro and the --use-vma option --- include/patchelfcrc/reporting.h | 3 ++- man/patchelfcrc.1.md | 5 ++++- src/main.c | 15 +++++++++++++-- src/xml.c | 6 +++--- 4 files changed, 22 insertions(+), 7 deletions(-) diff --git a/include/patchelfcrc/reporting.h b/include/patchelfcrc/reporting.h index ff682e8..e6a3cb2 100644 --- a/include/patchelfcrc/reporting.h +++ b/include/patchelfcrc/reporting.h @@ -21,7 +21,8 @@ #include -#define print_err(fmt, ...) fprintf(stderr, "[ERR] " fmt, ## __VA_ARGS__); +#define print_err(fmt, ...) fprintf(stderr, "[ERR] " fmt, ## __VA_ARGS__) +#define print_warn(fmt, ...) fprintf(stderr, "[WARN] " fmt, ## __VAR__ARGS__) void print_debug(const char *fmt, ...); diff --git a/man/patchelfcrc.1.md b/man/patchelfcrc.1.md index 63cdf7a..89ae5e9 100644 --- a/man/patchelfcrc.1.md +++ b/man/patchelfcrc.1.md @@ -11,7 +11,7 @@ [**\--granularity**=*GRANULARITY*] [**\--little-endian**] [**\--dry-run**] [**\--xsd**] [**\--poly**=*POLYNOMIAL*] [**\--reversed**] [**\--start-value**=*STARTVALUE*] [**--verbose**] [**\--xor-out**=*XORVAL*] [**\--end-magic**=*MAGIC*] -[**\--crc-format**=*FORMAT*] [**\--list-crcs**] [**\--output-section**=*OUTPUTSECTION*] +[**\--crc-format**=*FORMAT*] [**\--use-vma**] [**\--list-crcs**] [**\--output-section**=*OUTPUTSECTION*] [**\--start-magic**=*MAGIC*] [**\--section**=*SECTION*] [**\--help**] [**\--usage**] [**\--version**] *ELF* @@ -50,6 +50,9 @@ **-F** *FORMAT*, **\--crc-format**=*FORMAT* : Output format to place in output section. Options for *FORMAT* are *bare* or *struct* +**--use_vma** +: Use the virtual memory address (VMA) instead of the load memory address (LMA) for the address fields in the struct output. This option is only considered if the format is *struct* + **--start-magic**=*MAGIC*, **--endmagic**=*MAGIC* : *MAGIC* numbers (32 bit unsigned) that are expected to be found at the start and the end of the given output section. This serves as safety guard against accidental corruption of the output file. *It is highly recommended to use these options*. diff --git a/src/main.c b/src/main.c index c50a441..3a6e7da 100644 --- a/src/main.c +++ b/src/main.c @@ -38,13 +38,15 @@ const char *argp_program_bug_address = "print_xsd = true; break; + case ARG_KEY_USE_VMA: + args->use_vma = true; + break; case 'p': /* Polyniomial */ args->crc.polynomial = strtoull(arg, &endptr, 0); @@ -189,6 +194,7 @@ static int parse_cmdline_options(int *argc, char ***argv, struct command_line_op {"export", ARG_KEY_EXPORT, "XML", 0, "Export CRCs to XML file", 3}, {"import", ARG_KEY_IMPORT, "XML", 0, "Do not caclulate CRCs but import them from file", 3}, {"xsd", ARG_KEY_XSD, 0, 0, "Print XSD to stdout", 0}, + {"use-vma", ARG_KEY_USE_VMA, 0, 0, "Use the VMA instead of the LMA for struct output", 2}, /* Sentinel */ {NULL, 0, 0, 0, NULL, 0} }; @@ -214,6 +220,7 @@ static void prepare_default_opts(struct command_line_options *opts) opts->granularity = GRANULARITY_BYTE; opts->dry_run = false; opts->crc.xor = 0UL; + opts->use_vma = false; opts->crc.polynomial = 0x104C11DB7UL; opts->crc.start_value = 0xFFFFFFFFUL; opts->crc.rev = false; @@ -308,7 +315,7 @@ static int check_all_sections_present(elfpatch_handle_t *ep, SlList *list) if (!ep) return -1001; if (!list) { - print_err("No input sections specified.\n") + print_err("No input sections specified.\n"); return -1; } for (iter = list; iter; iter = sl_list_next(iter)) { @@ -426,6 +433,10 @@ int main(int argc, char **argv) return -2; } + if (cmd_opts.use_vma && cmd_opts.format != FORMAT_STRUCT) { + print_warn("--use-vma option only has an effect when exporting as struct output."); + } + if (!cmd_opts.output_section && cmd_opts.export_xml == NULL) { print_err("No output section / XML export specified. Will continue but not create any output\n"); } diff --git a/src/xml.c b/src/xml.c index a68e4bd..2ead328 100644 --- a/src/xml.c +++ b/src/xml.c @@ -39,8 +39,8 @@ int xml_write_crcs_to_file(const char *path, const uint32_t *crcs, SlList *secti writer = xmlNewTextWriterFilename(path, 0); if (!writer) { - print_err("Cannot create XML file %s\n", path) - ret = -1; + print_err("Cannot create XML file %s\n", path); + ret = -1; goto ret_none; } @@ -393,7 +393,7 @@ struct xml_crc_import *xml_import_from_file(const char *path) /* Get all CRCs */ xpath_obj = xmlXPathEvalExpression(BAD_CAST "/patchelfcrc/sections/crc", xpath_ctx); if (xmlXPathNodeSetIsEmpty(xpath_obj->nodesetval)) { - print_err("Internal error during read\n") + print_err("Internal error during read\n"); xml_crc_import_free(ret); ret = NULL; goto ret_close_doc;