public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Andrew Scull <ascull@google.com>
To: u-boot@lists.denx.de
Cc: sjg@chromium.org, seanga2@gmail.com, Andrew Scull <ascull@google.com>
Subject: [PATCH 02/11] sandbox: Migrate getopt section to linker list
Date: Thu,  7 Apr 2022 09:41:14 +0000	[thread overview]
Message-ID: <20220407094123.1752236-3-ascull@google.com> (raw)
In-Reply-To: <20220407094123.1752236-1-ascull@google.com>

Use the common infrastructure to create a linker list of the sandbox
command line flags rather than using a custom method.

The list is changed from containing pointers to containing structs and
the uses are updated accordingly.

Signed-off-by: Andrew Scull <ascull@google.com>
---
 arch/sandbox/cpu/os.c               | 21 ++++++++++-----------
 arch/sandbox/cpu/start.c            | 10 +++++-----
 arch/sandbox/cpu/u-boot-spl.lds     |  6 ------
 arch/sandbox/cpu/u-boot.lds         |  6 ------
 arch/sandbox/include/asm/getopt.h   | 19 ++++++++++++-------
 arch/sandbox/include/asm/sections.h | 25 -------------------------
 6 files changed, 27 insertions(+), 60 deletions(-)

diff --git a/arch/sandbox/cpu/os.c b/arch/sandbox/cpu/os.c
index d83c862182..72a72029f2 100644
--- a/arch/sandbox/cpu/os.c
+++ b/arch/sandbox/cpu/os.c
@@ -424,9 +424,8 @@ static struct option *long_opts;
 
 int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
 {
-	struct sandbox_cmdline_option **sb_opt =
-		__u_boot_sandbox_option_start();
-	size_t num_options = __u_boot_sandbox_option_count();
+	struct sandbox_cmdline_option *sb_opt = SANDBOX_CMDLINE_OPT_START();
+	size_t num_options = SANDBOX_CMDLINE_OPT_COUNT();
 	size_t i;
 
 	int hidden_short_opt;
@@ -455,17 +454,17 @@ int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
 	hidden_short_opt = 0x100;
 	si = 0;
 	for (i = 0; i < num_options; ++i) {
-		long_opts[i].name = sb_opt[i]->flag;
-		long_opts[i].has_arg = sb_opt[i]->has_arg ?
+		long_opts[i].name = sb_opt[i].flag;
+		long_opts[i].has_arg = sb_opt[i].has_arg ?
 			required_argument : no_argument;
 		long_opts[i].flag = NULL;
 
-		if (sb_opt[i]->flag_short) {
-			short_opts[si++] = long_opts[i].val = sb_opt[i]->flag_short;
+		if (sb_opt[i].flag_short) {
+			short_opts[si++] = long_opts[i].val = sb_opt[i].flag_short;
 			if (long_opts[i].has_arg == required_argument)
 				short_opts[si++] = ':';
 		} else
-			long_opts[i].val = sb_opt[i]->flag_short = hidden_short_opt++;
+			long_opts[i].val = sb_opt[i].flag_short = hidden_short_opt++;
 	}
 	short_opts[si] = '\0';
 
@@ -480,9 +479,9 @@ int os_parse_args(struct sandbox_state *state, int argc, char *argv[])
 	 */
 	while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != -1) {
 		for (i = 0; i < num_options; ++i) {
-			if (sb_opt[i]->flag_short == c) {
-				if (sb_opt[i]->callback(state, optarg)) {
-					state->parse_err = sb_opt[i]->flag;
+			if (sb_opt[i].flag_short == c) {
+				if (sb_opt[i].callback(state, optarg)) {
+					state->parse_err = sb_opt[i].flag;
 					return 0;
 				}
 				break;
diff --git a/arch/sandbox/cpu/start.c b/arch/sandbox/cpu/start.c
index 13b0731ec3..5cb47e1156 100644
--- a/arch/sandbox/cpu/start.c
+++ b/arch/sandbox/cpu/start.c
@@ -58,9 +58,8 @@ static int h_compare_opt(const void *p1, const void *p2)
 int sandbox_early_getopt_check(void)
 {
 	struct sandbox_state *state = state_get_current();
-	struct sandbox_cmdline_option **sb_opt =
-		__u_boot_sandbox_option_start();
-	size_t num_options = __u_boot_sandbox_option_count();
+	struct sandbox_cmdline_option *sb_opt = SANDBOX_CMDLINE_OPT_START();
+	size_t num_options = SANDBOX_CMDLINE_OPT_COUNT();
 	size_t i;
 	int max_arg_len, max_noarg_len;
 	struct sandbox_cmdline_option **sorted_opt;
@@ -84,7 +83,7 @@ int sandbox_early_getopt_check(void)
 
 	max_arg_len = 0;
 	for (i = 0; i < num_options; ++i)
-		max_arg_len = max((int)strlen(sb_opt[i]->flag), max_arg_len);
+		max_arg_len = max((int)strlen(sb_opt[i].flag), max_arg_len);
 	max_noarg_len = max_arg_len + 7;
 
 	/* Sort the options */
@@ -94,7 +93,8 @@ int sandbox_early_getopt_check(void)
 		printf("No memory to sort options\n");
 		os_exit(1);
 	}
-	memcpy(sorted_opt, sb_opt, size);
+	for (i = 0; i < num_options; ++i)
+		sorted_opt[i] = &sb_opt[i];
 	qsort(sorted_opt, num_options, sizeof(*sorted_opt), h_compare_opt);
 
 	for (i = 0; i < num_options; ++i) {
diff --git a/arch/sandbox/cpu/u-boot-spl.lds b/arch/sandbox/cpu/u-boot-spl.lds
index 6754f4ef6c..5c19d090cb 100644
--- a/arch/sandbox/cpu/u-boot-spl.lds
+++ b/arch/sandbox/cpu/u-boot-spl.lds
@@ -20,12 +20,6 @@ SECTIONS
 		*(SORT_BY_ALIGNMENT(SORT_BY_NAME(.priv_data*)))
 		__priv_data_end = .;
 	}
-
-	_u_boot_sandbox_getopt : {
-		*(.u_boot_sandbox_getopt_start)
-		KEEP(*(.u_boot_sandbox_getopt))
-		*(.u_boot_sandbox_getopt_end)
-	}
 }
 
 INSERT AFTER .data;
diff --git a/arch/sandbox/cpu/u-boot.lds b/arch/sandbox/cpu/u-boot.lds
index dd675cc3d2..c3fc67a15b 100644
--- a/arch/sandbox/cpu/u-boot.lds
+++ b/arch/sandbox/cpu/u-boot.lds
@@ -13,12 +13,6 @@ SECTIONS
 		KEEP(*(SORT(.u_boot_list*)));
 	}
 
-	_u_boot_sandbox_getopt : {
-		*(.u_boot_sandbox_getopt_start)
-		*(.u_boot_sandbox_getopt)
-		*(.u_boot_sandbox_getopt_end)
-	}
-
 	/* Sandbox has empty EFI runtime lists. */
 	__efi_runtime_start = .;
 	__efi_runtime_stop = __efi_runtime_start;
diff --git a/arch/sandbox/include/asm/getopt.h b/arch/sandbox/include/asm/getopt.h
index d2145ad6e2..a4b510bd20 100644
--- a/arch/sandbox/include/asm/getopt.h
+++ b/arch/sandbox/include/asm/getopt.h
@@ -9,6 +9,8 @@
 #ifndef __SANDBOX_GETOPT_H
 #define __SANDBOX_GETOPT_H
 
+#include <linker_lists.h>
+
 struct sandbox_state;
 
 /*
@@ -36,18 +38,13 @@ struct sandbox_cmdline_option {
  * magic junk that makes this all work.
  */
 #define _SANDBOX_CMDLINE_OPT(f, s, ha, h) \
-	static struct sandbox_cmdline_option sandbox_cmdline_option_##f = { \
+	ll_entry_declare(struct sandbox_cmdline_option, f, sandbox_getopt) = { \
 		.flag = #f, \
 		.flag_short = s, \
 		.help = h, \
 		.has_arg = ha, \
 		.callback = sandbox_cmdline_cb_##f, \
-	}; \
-	/* Ppointer to the struct in a special section for the linker script */ \
-	static __used __section(".u_boot_sandbox_getopt") \
-		struct sandbox_cmdline_option \
-			*sandbox_cmdline_option_##f##_ptr = \
-			&sandbox_cmdline_option_##f
+	}
 
 /**
  * Macros for end code to declare new command line flags.
@@ -69,4 +66,12 @@ struct sandbox_cmdline_option {
  */
 #define SANDBOX_CMDLINE_OPT_SHORT(f, s, ha, h) _SANDBOX_CMDLINE_OPT(f, s, ha, h)
 
+/** Get the start of the list of command line flags. */
+#define SANDBOX_CMDLINE_OPT_START() \
+	ll_entry_start(struct sandbox_cmdline_option, sandbox_getopt)
+
+/** Get the number of entries in the command line flags list. */
+#define SANDBOX_CMDLINE_OPT_COUNT() \
+	ll_entry_count(struct sandbox_cmdline_option, sandbox_getopt)
+
 #endif
diff --git a/arch/sandbox/include/asm/sections.h b/arch/sandbox/include/asm/sections.h
index f4351ae7db..c335cb2061 100644
--- a/arch/sandbox/include/asm/sections.h
+++ b/arch/sandbox/include/asm/sections.h
@@ -11,29 +11,4 @@
 
 #include <asm-generic/sections.h>
 
-struct sandbox_cmdline_option;
-
-static inline struct sandbox_cmdline_option **
-__u_boot_sandbox_option_start(void)
-{
-	static char start[0] __aligned(4) __attribute__((unused))
-		__section(".u_boot_sandbox_getopt_start");
-
-	return (struct sandbox_cmdline_option **)&start;
-}
-
-static inline struct sandbox_cmdline_option **
-__u_boot_sandbox_option_end(void)
-{
-	static char end[0] __aligned(4) __attribute__((unused))
-		__section(".u_boot_sandbox_getopt_end");
-
-	return (struct sandbox_cmdline_option **)&end;
-}
-
-static inline size_t __u_boot_sandbox_option_count(void)
-{
-	return __u_boot_sandbox_option_end() - __u_boot_sandbox_option_start();
-}
-
 #endif
-- 
2.35.1.1094.g7c7d902a7c-goog


  parent reply	other threads:[~2022-04-07  9:42 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-07  9:41 [PATCH 00/11] Fuzzing and ASAN for sandbox Andrew Scull
2022-04-07  9:41 ` [PATCH 01/11] sandbox: Set the EFI symbols in linker script Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-11 22:15   ` Heinrich Schuchardt
2022-04-11 22:37     ` Andrew Scull
2022-04-07  9:41 ` Andrew Scull [this message]
2022-04-11 18:35   ` [PATCH 02/11] sandbox: Migrate getopt section to linker list Simon Glass
2022-04-07  9:41 ` [PATCH 03/11] linker_lists: Rename sections to remove . prefix Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-07  9:41 ` [PATCH 04/11] sandbox: Add support for Address Sanitizer Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-12  9:26     ` Andrew Scull
2022-04-07  9:41 ` [PATCH 05/11] fuzzing_engine: Add fuzzing engine uclass Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-07  9:41 ` [PATCH 06/11] test: fuzz: Add framework for fuzzing Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-07  9:41 ` [PATCH 07/11] sandbox: Decouple program entry from sandbox init Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-07  9:41 ` [PATCH 08/11] sandbox: Add libfuzzer integration Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-07  9:41 ` [PATCH 09/11] sandbox: Implement fuzzing engine driver Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-14 13:44     ` Andrew Scull
2022-04-07  9:41 ` [PATCH 10/11] fuzz: virtio: Add fuzzer for vring Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-12 14:04     ` Andrew Scull
2022-04-07  9:41 ` [PATCH 11/11] RFC: Hack dlmalloc to poison memory Andrew Scull
2022-04-11 18:35   ` Simon Glass
2022-04-12 10:19     ` Andrew Scull

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220407094123.1752236-3-ascull@google.com \
    --to=ascull@google.com \
    --cc=seanga2@gmail.com \
    --cc=sjg@chromium.org \
    --cc=u-boot@lists.denx.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox