mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: David Dgien <dgienda125@gmail.com>
To: barebox@lists.infradead.org
Cc: David Dgien <dgienda125@gmail.com>
Subject: [RFC PATCH 4/8] module: Fix module command registration
Date: Tue, 16 Jun 2020 23:44:00 -0400	[thread overview]
Message-ID: <20200617034404.5904-5-dgienda125@gmail.com> (raw)
In-Reply-To: <20200617034404.5904-1-dgienda125@gmail.com>

Commit 1e4a948673d7 ("command: Use array of pointers to commands")
updated the command linker sections to contain an array of pointers to
the command structs, rather than the command structs themselves.
However, the load_module function was not updated to account for the
change to the linker section, so fix that here.

Additionally, commit 22bdecc9c591 ("lds: Move start/end address
variables into defines") moved the _start and _end symbols out of the
linker script and into the section macro defines, causing them to show
up in the module symbol table as well, so we take can advantage of those
to find the array of pointers.

Fixes: 1e4a948673d7 ("command: Use array of pointers to commands")
Signed-off-by: David Dgien <dgienda125@gmail.com>
---
 common/module.c | 43 +++++++++++++++++++++++++++++--------------
 1 file changed, 29 insertions(+), 14 deletions(-)

diff --git a/common/module.c b/common/module.c
index 5ace544e0..b08df1199 100644
--- a/common/module.c
+++ b/common/module.c
@@ -176,6 +176,34 @@ static void layout_sections( struct module *mod,
 	debug("core_size: %ld\n", mod->core_size);
 }
 
+static void register_module_cmds(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex)
+{
+	Elf32_Sym *sym;
+	unsigned int numsyms;
+	unsigned int i;
+	struct command * const *cmd_start = NULL;
+	struct command * const *cmd_end = NULL;
+	struct command * const *cmd;
+
+	numsyms = sechdrs[symindex].sh_size / sizeof(Elf32_Sym);
+	sym = (void *)sechdrs[symindex].sh_addr;
+
+	for (i = 0; i < numsyms; i++) {
+		if (strcmp(strtab + sym[i].st_name, MODULE_SYMBOL_PREFIX "__barebox_cmd_start") == 0)
+			cmd_start = (struct command * const *)sym[i].st_value;
+
+		if (strcmp(strtab + sym[i].st_name, MODULE_SYMBOL_PREFIX "__barebox_cmd_end") == 0)
+			cmd_end = (struct command * const *)sym[i].st_value;
+	}
+
+	if (cmd_start && cmd_end) {
+		debug("found __barebox_cmd_start at 0x%08x\n", (uint32_t)cmd_start);
+		for (cmd = cmd_start; cmd != cmd_end; cmd++) {
+			register_command(*cmd);
+		}
+	}
+}
+
 LIST_HEAD(module_list);
 
 struct module * load_module(void *mod_image, unsigned long len)
@@ -183,8 +211,6 @@ struct module * load_module(void *mod_image, unsigned long len)
 	struct module *module = NULL;
 	Elf32_Ehdr *ehdr;		/* Elf header structure pointer     */
 	Elf32_Shdr *sechdrs;		/* Section header structure pointer */
-	Elf32_Sym *sym;
-	unsigned int numsyms;
 	char *strtab = 0;		/* String table pointer             */
 	int i;				/* Loop counter                     */
 	unsigned int strindex = 0;
@@ -193,7 +219,6 @@ struct module * load_module(void *mod_image, unsigned long len)
 	char *secstrings;
 	void *ptr = NULL;
 	int err;
-	int cmdindex;
 
 	if (len < sizeof(*ehdr))
 		return NULL;
@@ -285,17 +310,7 @@ struct module * load_module(void *mod_image, unsigned long len)
 			apply_relocate_add(sechdrs, strtab, symindex, i, module);
 	}
 
-	numsyms = sechdrs[symindex].sh_size / sizeof(Elf32_Sym);
-	sym = (void *)sechdrs[symindex].sh_addr;
-
-	cmdindex = find_sec(ehdr, sechdrs, secstrings, ".barebox_cmd");
-	if (cmdindex) {
-		struct command *cmd =(struct command *)sechdrs[cmdindex].sh_addr;
-		for (i = 0; i < sechdrs[cmdindex].sh_size / sizeof(struct command); i++) {
-			register_command(cmd);
-			cmd++;
-		}
-	}
+	register_module_cmds(sechdrs, strtab, symindex);
 
 	/* Module has been moved */
 	module = (void *)sechdrs[modindex].sh_addr;
-- 
2.27.0


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

  parent reply	other threads:[~2020-06-17  3:43 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-17  3:43 [RFC PATCH 0/8] Module and ARM Module updates and fixes David Dgien
2020-06-17  3:43 ` [RFC PATCH 1/8] Makefile: Initialize and export KBUILD variables David Dgien
2020-06-17  3:43 ` [RFC PATCH 2/8] module: Add init macros to module.h David Dgien
2020-06-17  3:43 ` [RFC PATCH 3/8] module: Fix adding module to list after layout David Dgien
2020-06-17  3:44 ` David Dgien [this message]
2020-06-17  3:44 ` [RFC PATCH 5/8] module: Implement HAVE_MOD_ARCH_SPECIFIC David Dgien
2020-06-17  3:44 ` [RFC PATCH 6/8] arm: makefile: Fix compiler flag variable David Dgien
2020-06-17  3:44 ` [RFC PATCH 7/8] arm: elf: Add THM relocation types David Dgien
2020-06-17  3:44 ` [RFC PATCH 8/8] arm: module: Allow modules outside of bl range David Dgien
2020-06-17 13:52   ` Sascha Hauer
2020-06-17 13:45 ` [RFC PATCH 0/8] Module and ARM Module updates and fixes Sascha Hauer
2020-06-18  1:54   ` David Dgien
2020-06-18 13:10     ` Sascha Hauer
2020-06-22 17:52       ` Masahiro Yamada

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=20200617034404.5904-5-dgienda125@gmail.com \
    --to=dgienda125@gmail.com \
    --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