[RFC,50/73] x86/tools/relocs: Cleanup cmdline options

Message ID 20240226143630.33643-51-jiangshanlai@gmail.com
State New
Headers
Series [RFC,01/73] KVM: Documentation: Add the specification for PVM |

Commit Message

Lai Jiangshan Feb. 26, 2024, 2:36 p.m. UTC
  From: Hou Wenlong <houwenlong.hwl@antgroup.com>

Collect all cmdline options into a structure to make code
clean and to be easy to add new cmdline option.

Signed-off-by: Hou Wenlong <houwenlong.hwl@antgroup.com>
Signed-off-by: Lai Jiangshan <jiangshan.ljs@antgroup.com>
---
 arch/x86/tools/relocs.c        | 30 ++++++++++++++----------------
 arch/x86/tools/relocs.h        | 19 +++++++++++++------
 arch/x86/tools/relocs_common.c | 27 +++++++++------------------
 3 files changed, 36 insertions(+), 40 deletions(-)
  

Patch

diff --git a/arch/x86/tools/relocs.c b/arch/x86/tools/relocs.c
index cf28a8f05375..743e5e44338b 100644
--- a/arch/x86/tools/relocs.c
+++ b/arch/x86/tools/relocs.c
@@ -125,13 +125,13 @@  static int is_reloc(enum symtype type, const char *sym_name)
 		!regexec(&sym_regex_c[type], sym_name, 0, NULL, 0);
 }
 
-static void regex_init(int use_real_mode)
+static void regex_init(void)
 {
         char errbuf[128];
         int err;
 	int i;
 
-	if (use_real_mode)
+	if (opts.use_real_mode)
 		sym_regex = sym_regex_realmode;
 	else
 		sym_regex = sym_regex_kernel;
@@ -1164,7 +1164,7 @@  static int write32_as_text(uint32_t v, FILE *f)
 	return fprintf(f, "\t.long 0x%08"PRIx32"\n", v) > 0 ? 0 : -1;
 }
 
-static void emit_relocs(int as_text, int use_real_mode)
+static void emit_relocs(void)
 {
 	int i;
 	int (*write_reloc)(uint32_t, FILE *) = write32;
@@ -1172,12 +1172,12 @@  static void emit_relocs(int as_text, int use_real_mode)
 			const char *symname);
 
 #if ELF_BITS == 64
-	if (!use_real_mode)
+	if (!opts.use_real_mode)
 		do_reloc = do_reloc64;
 	else
 		die("--realmode not valid for a 64-bit ELF file");
 #else
-	if (!use_real_mode)
+	if (!opts.use_real_mode)
 		do_reloc = do_reloc32;
 	else
 		do_reloc = do_reloc_real;
@@ -1186,7 +1186,7 @@  static void emit_relocs(int as_text, int use_real_mode)
 	/* Collect up the relocations */
 	walk_relocs(do_reloc);
 
-	if (relocs16.count && !use_real_mode)
+	if (relocs16.count && !opts.use_real_mode)
 		die("Segment relocations found but --realmode not specified\n");
 
 	/* Order the relocations for more efficient processing */
@@ -1199,7 +1199,7 @@  static void emit_relocs(int as_text, int use_real_mode)
 #endif
 
 	/* Print the relocations */
-	if (as_text) {
+	if (opts.as_text) {
 		/* Print the relocations in a form suitable that
 		 * gas will like.
 		 */
@@ -1208,7 +1208,7 @@  static void emit_relocs(int as_text, int use_real_mode)
 		write_reloc = write32_as_text;
 	}
 
-	if (use_real_mode) {
+	if (opts.use_real_mode) {
 		write_reloc(relocs16.count, stdout);
 		for (i = 0; i < relocs16.count; i++)
 			write_reloc(relocs16.offset[i], stdout);
@@ -1271,11 +1271,9 @@  static void print_reloc_info(void)
 # define process process_32
 #endif
 
-void process(FILE *fp, int use_real_mode, int as_text,
-	     int show_absolute_syms, int show_absolute_relocs,
-	     int show_reloc_info)
+void process(FILE *fp)
 {
-	regex_init(use_real_mode);
+	regex_init();
 	read_ehdr(fp);
 	read_shdrs(fp);
 	read_strtabs(fp);
@@ -1284,17 +1282,17 @@  void process(FILE *fp, int use_real_mode, int as_text,
 	read_got(fp);
 	if (ELF_BITS == 64)
 		percpu_init();
-	if (show_absolute_syms) {
+	if (opts.show_absolute_syms) {
 		print_absolute_symbols();
 		return;
 	}
-	if (show_absolute_relocs) {
+	if (opts.show_absolute_relocs) {
 		print_absolute_relocs();
 		return;
 	}
-	if (show_reloc_info) {
+	if (opts.show_reloc_info) {
 		print_reloc_info();
 		return;
 	}
-	emit_relocs(as_text, use_real_mode);
+	emit_relocs();
 }
diff --git a/arch/x86/tools/relocs.h b/arch/x86/tools/relocs.h
index 4c49c82446eb..1cb0e235ad73 100644
--- a/arch/x86/tools/relocs.h
+++ b/arch/x86/tools/relocs.h
@@ -6,6 +6,7 @@ 
 #include <stdarg.h>
 #include <stdlib.h>
 #include <stdint.h>
+#include <stdbool.h>
 #include <inttypes.h>
 #include <string.h>
 #include <errno.h>
@@ -30,10 +31,16 @@  enum symtype {
 	S_NSYMTYPES
 };
 
-void process_32(FILE *fp, int use_real_mode, int as_text,
-		int show_absolute_syms, int show_absolute_relocs,
-		int show_reloc_info);
-void process_64(FILE *fp, int use_real_mode, int as_text,
-		int show_absolute_syms, int show_absolute_relocs,
-		int show_reloc_info);
+struct opts {
+	bool use_real_mode;
+	bool as_text;
+	bool show_absolute_syms;
+	bool show_absolute_relocs;
+	bool show_reloc_info;
+};
+
+extern struct opts opts;
+
+void process_32(FILE *fp);
+void process_64(FILE *fp);
 #endif /* RELOCS_H */
diff --git a/arch/x86/tools/relocs_common.c b/arch/x86/tools/relocs_common.c
index 6634352a20bc..17d69baee0c3 100644
--- a/arch/x86/tools/relocs_common.c
+++ b/arch/x86/tools/relocs_common.c
@@ -1,6 +1,8 @@ 
 // SPDX-License-Identifier: GPL-2.0
 #include "relocs.h"
 
+struct opts opts;
+
 void die(char *fmt, ...)
 {
 	va_list ap;
@@ -18,40 +20,33 @@  static void usage(void)
 
 int main(int argc, char **argv)
 {
-	int show_absolute_syms, show_absolute_relocs, show_reloc_info;
-	int as_text, use_real_mode;
 	const char *fname;
 	FILE *fp;
 	int i;
 	unsigned char e_ident[EI_NIDENT];
 
-	show_absolute_syms = 0;
-	show_absolute_relocs = 0;
-	show_reloc_info = 0;
-	as_text = 0;
-	use_real_mode = 0;
 	fname = NULL;
 	for (i = 1; i < argc; i++) {
 		char *arg = argv[i];
 		if (*arg == '-') {
 			if (strcmp(arg, "--abs-syms") == 0) {
-				show_absolute_syms = 1;
+				opts.show_absolute_syms = true;
 				continue;
 			}
 			if (strcmp(arg, "--abs-relocs") == 0) {
-				show_absolute_relocs = 1;
+				opts.show_absolute_relocs = true;
 				continue;
 			}
 			if (strcmp(arg, "--reloc-info") == 0) {
-				show_reloc_info = 1;
+				opts.show_reloc_info = true;
 				continue;
 			}
 			if (strcmp(arg, "--text") == 0) {
-				as_text = 1;
+				opts.as_text = true;
 				continue;
 			}
 			if (strcmp(arg, "--realmode") == 0) {
-				use_real_mode = 1;
+				opts.use_real_mode = true;
 				continue;
 			}
 		}
@@ -73,13 +68,9 @@  int main(int argc, char **argv)
 	}
 	rewind(fp);
 	if (e_ident[EI_CLASS] == ELFCLASS64)
-		process_64(fp, use_real_mode, as_text,
-			   show_absolute_syms, show_absolute_relocs,
-			   show_reloc_info);
+		process_64(fp);
 	else
-		process_32(fp, use_real_mode, as_text,
-			   show_absolute_syms, show_absolute_relocs,
-			   show_reloc_info);
+		process_32(fp);
 	fclose(fp);
 	return 0;
 }