Skip to content

Commit 6545eb0

Browse files
Julien Thierryjpoimboe
authored andcommitted
objtool: Move object file loading out of check()
Structure objtool_file can be used by different subcommands. In fact it already is, by check and orc. Provide a function that allows to initialize objtool_file, that builtin can call, without relying on check to do the correct setup for them and explicitly hand the objtool_file to them. Reviewed-by: Miroslav Benes <mbenes@suse.cz> Signed-off-by: Julien Thierry <jthierry@redhat.com> Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
1 parent 7c9903c commit 6545eb0

6 files changed

Lines changed: 60 additions & 35 deletions

File tree

tools/objtool/builtin-check.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const struct option check_options[] = {
4141
int cmd_check(int argc, const char **argv)
4242
{
4343
const char *objname, *s;
44+
struct objtool_file *file;
4445

4546
argc = parse_options(argc, argv, check_options, check_usage, 0);
4647

@@ -53,5 +54,9 @@ int cmd_check(int argc, const char **argv)
5354
if (s && !s[9])
5455
vmlinux = true;
5556

56-
return check(objname, false);
57+
file = objtool_open_read(objname);
58+
if (!file)
59+
return 1;
60+
61+
return check(file, false);
5762
}

tools/objtool/builtin-orc.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ int cmd_orc(int argc, const char **argv)
3131
usage_with_options(orc_usage, check_options);
3232

3333
if (!strncmp(argv[0], "gen", 3)) {
34+
struct objtool_file *file;
35+
3436
argc = parse_options(argc, argv, check_options, orc_usage, 0);
3537
if (argc != 1)
3638
usage_with_options(orc_usage, check_options);
3739

3840
objname = argv[0];
3941

40-
return check(objname, true);
42+
file = objtool_open_read(objname);
43+
if (!file)
44+
return 1;
45+
46+
return check(file, true);
4147
}
4248

4349
if (!strcmp(argv[0], "dump")) {

tools/objtool/check.c

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ struct alternative {
2828
bool skip_orig;
2929
};
3030

31-
const char *objname;
3231
struct cfi_init_state initial_func_cfi;
3332

3433
struct instruction *find_insn(struct objtool_file *file,
@@ -2909,37 +2908,22 @@ static int validate_reachable_instructions(struct objtool_file *file)
29092908
return 0;
29102909
}
29112910

2912-
static struct objtool_file file;
2913-
2914-
int check(const char *_objname, bool orc)
2911+
int check(struct objtool_file *file, bool orc)
29152912
{
29162913
int ret, warnings = 0;
29172914

2918-
objname = _objname;
2919-
2920-
file.elf = elf_open_read(objname, O_RDWR);
2921-
if (!file.elf)
2922-
return 1;
2923-
2924-
INIT_LIST_HEAD(&file.insn_list);
2925-
hash_init(file.insn_hash);
2926-
INIT_LIST_HEAD(&file.static_call_list);
2927-
file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
2928-
file.ignore_unreachables = no_unreachable;
2929-
file.hints = false;
2930-
29312915
arch_initial_func_cfi_state(&initial_func_cfi);
29322916

2933-
ret = decode_sections(&file);
2917+
ret = decode_sections(file);
29342918
if (ret < 0)
29352919
goto out;
29362920
warnings += ret;
29372921

2938-
if (list_empty(&file.insn_list))
2922+
if (list_empty(&file->insn_list))
29392923
goto out;
29402924

29412925
if (vmlinux && !validate_dup) {
2942-
ret = validate_vmlinux_functions(&file);
2926+
ret = validate_vmlinux_functions(file);
29432927
if (ret < 0)
29442928
goto out;
29452929

@@ -2948,46 +2932,46 @@ int check(const char *_objname, bool orc)
29482932
}
29492933

29502934
if (retpoline) {
2951-
ret = validate_retpoline(&file);
2935+
ret = validate_retpoline(file);
29522936
if (ret < 0)
29532937
return ret;
29542938
warnings += ret;
29552939
}
29562940

2957-
ret = validate_functions(&file);
2941+
ret = validate_functions(file);
29582942
if (ret < 0)
29592943
goto out;
29602944
warnings += ret;
29612945

2962-
ret = validate_unwind_hints(&file, NULL);
2946+
ret = validate_unwind_hints(file, NULL);
29632947
if (ret < 0)
29642948
goto out;
29652949
warnings += ret;
29662950

29672951
if (!warnings) {
2968-
ret = validate_reachable_instructions(&file);
2952+
ret = validate_reachable_instructions(file);
29692953
if (ret < 0)
29702954
goto out;
29712955
warnings += ret;
29722956
}
29732957

2974-
ret = create_static_call_sections(&file);
2958+
ret = create_static_call_sections(file);
29752959
if (ret < 0)
29762960
goto out;
29772961
warnings += ret;
29782962

29792963
if (orc) {
2980-
ret = create_orc(&file);
2964+
ret = create_orc(file);
29812965
if (ret < 0)
29822966
goto out;
29832967

2984-
ret = create_orc_sections(&file);
2968+
ret = create_orc_sections(file);
29852969
if (ret < 0)
29862970
goto out;
29872971
}
29882972

2989-
if (file.elf->changed) {
2990-
ret = elf_write(file.elf);
2973+
if (file->elf->changed) {
2974+
ret = elf_write(file->elf);
29912975
if (ret < 0)
29922976
goto out;
29932977
}

tools/objtool/objtool.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/kernel.h>
2323

2424
#include "builtin.h"
25+
#include "objtool.h"
26+
#include "warn.h"
2527

2628
struct cmd_struct {
2729
const char *name;
@@ -39,6 +41,34 @@ static struct cmd_struct objtool_cmds[] = {
3941

4042
bool help;
4143

44+
const char *objname;
45+
static struct objtool_file file;
46+
47+
struct objtool_file *objtool_open_read(const char *_objname)
48+
{
49+
if (objname) {
50+
if (strcmp(objname, _objname)) {
51+
WARN("won't handle more than one file at a time");
52+
return NULL;
53+
}
54+
return &file;
55+
}
56+
objname = _objname;
57+
58+
file.elf = elf_open_read(objname, O_RDWR);
59+
if (!file.elf)
60+
return NULL;
61+
62+
INIT_LIST_HEAD(&file.insn_list);
63+
hash_init(file.insn_hash);
64+
INIT_LIST_HEAD(&file.static_call_list);
65+
file.c_file = !vmlinux && find_section_by_name(file.elf, ".comment");
66+
file.ignore_unreachables = no_unreachable;
67+
file.hints = false;
68+
69+
return &file;
70+
}
71+
4272
static void cmd_usage(void)
4373
{
4474
unsigned int i, longest = 0;

tools/objtool/objtool.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ struct objtool_file {
2020
bool ignore_unreachables, c_file, hints, rodata;
2121
};
2222

23-
int check(const char *objname, bool orc);
23+
struct objtool_file *objtool_open_read(const char *_objname);
24+
25+
int check(struct objtool_file *file, bool orc);
2426
int orc_dump(const char *objname);
2527
int create_orc(struct objtool_file *file);
2628
int create_orc_sections(struct objtool_file *file);

tools/objtool/weak.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
return ENOSYS; \
1818
})
1919

20-
const char __weak *objname;
21-
22-
int __weak check(const char *_objname, bool orc)
20+
int __weak check(struct objtool_file *file, bool orc)
2321
{
2422
UNSUPPORTED("check subcommand");
2523
}

0 commit comments

Comments
 (0)