[RFC 02/13] objtool: Create disassembly context

Alexandre Chartre posted 13 patches 8 months ago
There is a newer version of this series
[RFC 02/13] objtool: Create disassembly context
Posted by Alexandre Chartre 8 months ago
Create a structure to store information for disassembling functions.
For now, it is just a wrapper around an objtool file.

Signed-off-by: Alexandre Chartre <alexandre.chartre@oracle.com>
---
 tools/objtool/check.c                   |  5 +++-
 tools/objtool/disas.c                   | 33 +++++++++++++++++++++++--
 tools/objtool/include/objtool/objtool.h |  5 +++-
 3 files changed, 39 insertions(+), 4 deletions(-)

diff --git a/tools/objtool/check.c b/tools/objtool/check.c
index bd1974717fa3..085fcc1b643b 100644
--- a/tools/objtool/check.c
+++ b/tools/objtool/check.c
@@ -4591,6 +4591,7 @@ static void free_insns(struct objtool_file *file)
 
 int check(struct objtool_file *file)
 {
+	struct disas_context *disas_ctx;
 	int ret = 0, warnings = 0;
 
 	arch_initial_func_cfi_state(&initial_func_cfi);
@@ -4720,7 +4721,9 @@ int check(struct objtool_file *file)
 		if (opts.werror && warnings)
 			WARN("%d warning(s) upgraded to errors", warnings);
 		print_args();
-		disas_warned_funcs(file);
+		disas_ctx = disas_context_create(file);
+		disas_warned_funcs(disas_ctx);
+		disas_context_destroy(disas_ctx);
 	}
 
 	return ret;
diff --git a/tools/objtool/disas.c b/tools/objtool/disas.c
index 77de46beb496..ed74554bccbf 100644
--- a/tools/objtool/disas.c
+++ b/tools/objtool/disas.c
@@ -8,6 +8,30 @@
 
 #include <linux/string.h>
 
+struct disas_context {
+	struct objtool_file *file;
+};
+
+struct disas_context *disas_context_create(struct objtool_file *file)
+{
+	struct disas_context *dctx;
+
+	dctx = malloc(sizeof(*dctx));
+	if (!dctx) {
+		WARN("failed too allocate disassembly context\n");
+		return NULL;
+	}
+
+	dctx->file = file;
+
+	return dctx;
+}
+
+void disas_context_destroy(struct disas_context *dctx)
+{
+	free(dctx);
+}
+
 /* 'funcs' is a space-separated list of function names */
 static void disas_funcs(const char *funcs)
 {
@@ -58,12 +82,17 @@ static void disas_funcs(const char *funcs)
 	}
 }
 
-void disas_warned_funcs(struct objtool_file *file)
+void disas_warned_funcs(struct disas_context *dctx)
 {
 	struct symbol *sym;
 	char *funcs = NULL, *tmp;
 
-	for_each_sym(file, sym) {
+	if (!dctx) {
+		ERROR("disassembly context is not defined");
+		return;
+	}
+
+	for_each_sym(dctx->file, sym) {
 		if (sym->warned) {
 			if (!funcs) {
 				funcs = malloc(strlen(sym->name) + 1);
diff --git a/tools/objtool/include/objtool/objtool.h b/tools/objtool/include/objtool/objtool.h
index 4d3e94b70fd8..f5ab71f07f5c 100644
--- a/tools/objtool/include/objtool/objtool.h
+++ b/tools/objtool/include/objtool/objtool.h
@@ -47,6 +47,9 @@ int check(struct objtool_file *file);
 int orc_dump(const char *objname);
 int orc_create(struct objtool_file *file);
 
-void disas_warned_funcs(struct objtool_file *file);
+struct disas_context;
+struct disas_context *disas_context_create(struct objtool_file *file);
+void disas_context_destroy(struct disas_context *dctx);
+void disas_warned_funcs(struct disas_context *dctx);
 
 #endif /* _OBJTOOL_H */
-- 
2.43.5
Re: [RFC 02/13] objtool: Create disassembly context
Posted by Josh Poimboeuf 8 months ago
On Fri, Jun 06, 2025 at 05:34:29PM +0200, Alexandre Chartre wrote:
> +struct disas_context *disas_context_create(struct objtool_file *file)
> +{
> +	struct disas_context *dctx;
> +
> +	dctx = malloc(sizeof(*dctx));
> +	if (!dctx) {
> +		WARN("failed too allocate disassembly context\n");
> +		return NULL;

"too" -> "to".

Also, no newline needed for objtool warnings/error strings.

Also, might want to use WARN_GLIBC() here.

> +void disas_context_destroy(struct disas_context *dctx)
> +{
> +	free(dctx);
> +}

In general we try to avoid freeing memory in objtool for performance
reasons, though this is the error path so I suppose it's harmless.

> -void disas_warned_funcs(struct objtool_file *file)
> +void disas_warned_funcs(struct disas_context *dctx)
>  {
>  	struct symbol *sym;
>  	char *funcs = NULL, *tmp;
>  
> -	for_each_sym(file, sym) {
> +	if (!dctx) {
> +		ERROR("disassembly context is not defined");
> +		return;

This will have come from the error in disas_context_create(), no need to
print an additional error.

> +++ b/tools/objtool/include/objtool/objtool.h
> @@ -47,6 +47,9 @@ int check(struct objtool_file *file);
>  int orc_dump(const char *objname);
>  int orc_create(struct objtool_file *file);
>  
> -void disas_warned_funcs(struct objtool_file *file);
> +struct disas_context;
> +struct disas_context *disas_context_create(struct objtool_file *file);
> +void disas_context_destroy(struct disas_context *dctx);
> +void disas_warned_funcs(struct disas_context *dctx);

Can these go in a new disas.h file?

-- 
Josh