From 3c0e4f74f16e840958c0b1a824cb1dfeba8491fa Mon Sep 17 00:00:00 2001 From: Christian Mauderer Date: Mon, 14 Feb 2022 16:14:32 +0100 Subject: bin2c: Add option for alignment Sometimes it's useful if structures are aligned. This patch add a -A alignment option. Note that this doesn't check for valid alignments. It accepts any positive number in decimal or hex format. If for example an alignment of 7 is specified, the compiler will complain that it is not a power of 2. But it's not really useful to duplicate this check here. --- misc/bin2c/rtems-bin2c.c | 42 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 38 insertions(+), 4 deletions(-) diff --git a/misc/bin2c/rtems-bin2c.c b/misc/bin2c/rtems-bin2c.c index 462ecf0..8d0e6a1 100644 --- a/misc/bin2c/rtems-bin2c.c +++ b/misc/bin2c/rtems-bin2c.c @@ -42,6 +42,7 @@ int verbose = 0; int zeroterminated = 0; int createC = 1; int createH = 1; +unsigned int align = 0; static void sanitize_file_name(char *p) { @@ -175,11 +176,22 @@ void process(const char *ifname, const char *ofname, const char *forced_name) /* print structure */ fprintf( ocfile, - "%s%sunsigned char %s[] = {\n ", + "%s%sunsigned char %s[] ", ((usestatic) ? "static " : ""), ((useconst) ? "const " : ""), buf ); + if (align > 0) { + fprintf( + ocfile, + "__attribute__(( __aligned__(%d) )) ", + align + ); + } + fprintf( + ocfile, + "= {\n " + ); int c, col = 1; while ((c = myfgetc(ifile)) != EOF) { if (col >= 78 - 6) { @@ -238,15 +250,22 @@ void process(const char *ifname, const char *ofname, const char *forced_name) /* print structure */ fprintf( ohfile, - "extern %s%sunsigned char %s[];", + "extern %s%sunsigned char %s[]", ((usestatic) ? "static " : ""), ((useconst) ? "const " : ""), buf ); + if (align > 0) { + fprintf( + ohfile, + " __attribute__(( __aligned__(%d) ))", + align + ); + }; /* print sizeof */ fprintf( ohfile, - "\n" + ";\n" "extern %s%ssize_t %s_size;\n", ((usestatic) ? "static " : ""), ((useconst) ? "const " : ""), @@ -274,7 +293,7 @@ void usage(void) { fprintf( stderr, - "usage: bin2c [-csvzCH] [-N name] \n" + "usage: bin2c [-csvzCH] [-N name] [-A alignment] \n" " is the binary file to convert\n" " should not have a .c or .h extension\n" "\n" @@ -285,6 +304,7 @@ void usage(void) " -H - create c-header only\n" " -C - create c-source file only\n" " -N - force name of data array\n" + " -A - add alignment - parameter can be a hexadecimal or decimal number\n" ); exit(1); } @@ -329,6 +349,20 @@ int main(int argc, char **argv) name = argv[1]; --argc; ++argv; + } else if (!strcmp(argv[1], "-A")) { + --argc; + ++argv; + if (argc <= 1) { + fprintf(stderr, "error: -A needs an alignment\n"); + usage(); + } + align = strtoul(argv[1], NULL, 0); + if (align == 0) { + fprintf(stderr, "error: Couldn't convert argument of -A\n"); + usage(); + } + --argc; + ++argv; } else { usage(); } -- cgit v1.2.3