xref: /linux/tools/objtool/objtool.c (revision 6b3f7af57881f6d6250c6dcc4d910fe8e855a607)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (C) 2015 Josh Poimboeuf <jpoimboe@redhat.com>
4  */
5 
6 #include <stdio.h>
7 #include <stdbool.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <unistd.h>
11 #include <subcmd/exec-cmd.h>
12 #include <subcmd/pager.h>
13 #include <linux/kernel.h>
14 
15 #include <objtool/builtin.h>
16 #include <objtool/objtool.h>
17 #include <objtool/warn.h>
18 
19 static struct objtool_file file;
20 
21 struct objtool_file *objtool_open_read(const char *filename)
22 {
23 	if (file.elf) {
24 		ERROR("won't handle more than one file at a time");
25 		return NULL;
26 	}
27 
28 	file.elf = elf_open_read(filename, O_RDWR);
29 	if (!file.elf)
30 		return NULL;
31 
32 	hash_init(file.insn_hash);
33 	INIT_LIST_HEAD(&file.retpoline_call_list);
34 	INIT_LIST_HEAD(&file.return_thunk_list);
35 	INIT_LIST_HEAD(&file.static_call_list);
36 	INIT_LIST_HEAD(&file.mcount_loc_list);
37 	INIT_LIST_HEAD(&file.endbr_list);
38 	INIT_LIST_HEAD(&file.call_list);
39 	file.ignore_unreachables = opts.no_unreachable;
40 	file.hints = false;
41 
42 	return &file;
43 }
44 
45 int objtool_pv_add(struct objtool_file *f, int idx, struct symbol *func)
46 {
47 	if (!opts.noinstr)
48 		return 0;
49 
50 	if (!f->pv_ops) {
51 		ERROR("paravirt confusion");
52 		return -1;
53 	}
54 
55 	/*
56 	 * These functions will be patched into native code,
57 	 * see paravirt_patch().
58 	 */
59 	if (!strcmp(func->name, "_paravirt_nop") ||
60 	    !strcmp(func->name, "_paravirt_ident_64"))
61 		return 0;
62 
63 	/* already added this function */
64 	if (!list_empty(&func->pv_target))
65 		return 0;
66 
67 	list_add(&func->pv_target, &f->pv_ops[idx].targets);
68 	f->pv_ops[idx].clean = false;
69 	return 0;
70 }
71 
72 char *top_level_dir(const char *file)
73 {
74 	ssize_t len, self_len, file_len;
75 	char self[PATH_MAX], *str;
76 	int i;
77 
78 	len = readlink("/proc/self/exe", self, sizeof(self) - 1);
79 	if (len <= 0)
80 		return NULL;
81 	self[len] = '\0';
82 
83 	for (i = 0; i < 3; i++) {
84 		char *s = strrchr(self, '/');
85 		if (!s)
86 			return NULL;
87 		*s = '\0';
88 	}
89 
90 	self_len = strlen(self);
91 	file_len = strlen(file);
92 
93 	str = malloc(self_len + file_len + 2);
94 	if (!str)
95 		return NULL;
96 
97 	memcpy(str, self, self_len);
98 	str[self_len] = '/';
99 	strcpy(str + self_len + 1, file);
100 
101 	return str;
102 }
103 
104 int main(int argc, const char **argv)
105 {
106 	static const char *UNUSED = "OBJTOOL_NOT_IMPLEMENTED";
107 
108 	if (init_signal_handler())
109 		return -1;
110 
111 	/* libsubcmd init */
112 	exec_cmd_init("objtool", UNUSED, UNUSED, UNUSED);
113 	pager_init(UNUSED);
114 
115 	if (argc > 1 && !strcmp(argv[1], "klp")) {
116 		argc--;
117 		argv++;
118 		return cmd_klp(argc, argv);
119 	}
120 
121 	return objtool_run(argc, argv);
122 }
123