mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <s.hauer@pengutronix.de>
To: Barebox List <barebox@lists.infradead.org>
Subject: [PATCH] command: Use array of pointers to commands
Date: Mon, 14 Oct 2019 12:05:47 +0200	[thread overview]
Message-ID: <20191014100547.17832-1-s.hauer@pengutronix.de> (raw)
In-Reply-To: <20191002185337.GB32491@ravnborg.org>

We used to store the commands as a linker array. One problem with this
is that on X86_64 for unknown reasons the linker uses a different struct
alignment than the compiler, so when we use the linker to compose the
array and the compiler to iterate over it we have to play tricks with
manually adjusting the alignment. The other problem is that we declare
the commands as const (and also put it in .rodata), but in fact we do
not treat it as const: we put the commands onto a list which modifies
the struct list_head list member of struct command.

With this patch we no longer put the command themselves into an array,
but instead create an array of pointers to the commands. This inherently
solves the second issue as well.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 common/command.c  |  8 ++++----
 include/command.h | 21 +++++++++------------
 2 files changed, 13 insertions(+), 16 deletions(-)

diff --git a/common/command.c b/common/command.c
index d9cc4a6d48..49845938ae 100644
--- a/common/command.c
+++ b/common/command.c
@@ -149,12 +149,12 @@ EXPORT_SYMBOL(find_cmd);
  */
 static int init_command_list(void)
 {
-	struct command *cmdtp;
+	struct command * const *cmdtp;
 
-	for (cmdtp = &__barebox_cmd_start;
-			cmdtp != &__barebox_cmd_end;
+	for (cmdtp = __barebox_cmd_start;
+			cmdtp != __barebox_cmd_end;
 			cmdtp++)
-		register_command(cmdtp);
+		register_command(*cmdtp);
 
 	return 0;
 }
diff --git a/include/command.h b/include/command.h
index 0afc5c7550..7555d0053c 100644
--- a/include/command.h
+++ b/include/command.h
@@ -53,15 +53,10 @@ struct command {
 	const char	*help;		/* Help  message	(long)	*/
 	void		(*usage)(void);
 #endif
-}
-#ifdef __x86_64__
-/* This is required because the linker will put symbols on a 64 bit alignment */
-__attribute__((aligned(64)))
-#endif
-;
+};
 
-extern struct command __barebox_cmd_start;
-extern struct command __barebox_cmd_end;
+extern struct command * const __barebox_cmd_start[];
+extern struct command * const __barebox_cmd_end[];
 
 
 /* common/command.c */
@@ -89,10 +84,12 @@ int run_command(const char *cmd);
 
 #endif	/* __ASSEMBLY__ */
 
-#define BAREBOX_CMD_START(_name)							\
-extern const struct command __barebox_cmd_##_name;					\
-const struct command __barebox_cmd_##_name						\
-	__attribute__ ((unused,section (".barebox_cmd_" __stringify(_name)))) = {	\
+#define BAREBOX_CMD_START(_name)						\
+static struct command __barebox_cmd_##_name;					\
+const struct command *barebox_cmd_##_name 					\
+	__attribute__ ((unused,section (".barebox_cmd_" __stringify(_name))))	\
+			= &__barebox_cmd_##_name;				\
+static struct command __barebox_cmd_##_name = {					\
 	.name		= #_name,
 
 #define BAREBOX_CMD_END					\
-- 
2.23.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  reply	other threads:[~2019-10-14 10:05 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-02 14:44 [PATCH 0/7] Cleanup linker scripts Sascha Hauer
2019-10-02 14:44 ` [PATCH 1/7] barebox.lds: Remove unnecessary braces Sascha Hauer
2019-10-02 14:44 ` [PATCH 2/7] ppc: remove unused variables from linker scripts Sascha Hauer
2019-10-02 14:44 ` [PATCH 3/7] lds: Move start/end address variables into defines Sascha Hauer
2019-10-02 18:46   ` Sam Ravnborg
2019-10-02 14:44 ` [PATCH 4/7] lds: create and use BAREBOX_PCI_FIXUP macro Sascha Hauer
2019-10-02 14:44 ` [PATCH 5/7] X86: lds: remove unnecessary alignments Sascha Hauer
2019-10-02 18:53   ` Sam Ravnborg
2019-10-14 10:05     ` Sascha Hauer [this message]
2019-10-14 10:08     ` Sascha Hauer
2019-10-02 14:44 ` [PATCH 6/7] lds: remove more " Sascha Hauer
2019-10-02 18:55   ` Sam Ravnborg
2019-10-02 14:44 ` [PATCH 7/7] lds: Add and use RO_DATA_SECTION macro Sascha Hauer

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=20191014100547.17832-1-s.hauer@pengutronix.de \
    --to=s.hauer@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    /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