1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3  * Copyright (C) 2017 Josh Poimboeuf <jpoimboe@redhat.com>
4  */
5 
6 #ifndef _CHECK_H
7 #define _CHECK_H
8 
9 #include <stdbool.h>
10 #include <objtool/cfi.h>
11 #include <objtool/arch.h>
12 
13 struct insn_state {
14 	struct cfi_state cfi;
15 	unsigned int uaccess_stack;
16 	bool uaccess;
17 	bool df;
18 	bool noinstr;
19 	s8 instr;
20 };
21 
22 struct alt_group {
23 	/*
24 	 * Pointer from a replacement group to the original group.  NULL if it
25 	 * *is* the original group.
26 	 */
27 	struct alt_group *orig_group;
28 
29 	/* First and last instructions in the group */
30 	struct instruction *first_insn, *last_insn, *nop;
31 
32 	/*
33 	 * Byte-offset-addressed len-sized array of pointers to CFI structs.
34 	 * This is shared with the other alt_groups in the same alternative.
35 	 */
36 	struct cfi_state **cfi;
37 
38 	bool ignore;
39 };
40 
41 #define INSN_CHUNK_BITS		8
42 #define INSN_CHUNK_SIZE		(1 << INSN_CHUNK_BITS)
43 #define INSN_CHUNK_MAX		(INSN_CHUNK_SIZE - 1)
44 
45 struct instruction {
46 	struct hlist_node hash;
47 	struct list_head call_node;
48 	struct section *sec;
49 	unsigned long offset;
50 	unsigned long immediate;
51 
52 	u8 len;
53 	u8 prev_len;
54 	u8 type;
55 	s8 instr;
56 
57 	u32 idx			: INSN_CHUNK_BITS,
58 	    dead_end		: 1,
59 	    ignore_alts		: 1,
60 	    hint		: 1,
61 	    save		: 1,
62 	    restore		: 1,
63 	    retpoline_safe	: 1,
64 	    noendbr		: 1,
65 	    unret		: 1,
66 	    visited		: 4,
67 	    no_reloc		: 1;
68 		/* 10 bit hole */
69 
70 	struct alt_group *alt_group;
71 	struct instruction *jump_dest;
72 	struct instruction *first_jump_src;
73 	union {
74 		struct symbol *_call_dest;
75 		struct {
76 			struct reloc *_jump_table;
77 			unsigned long _jump_table_size;
78 		};
79 	};
80 	struct alternative *alts;
81 	struct symbol *sym;
82 	struct stack_op *stack_ops;
83 	struct cfi_state *cfi;
84 };
85 
insn_func(struct instruction * insn)86 static inline struct symbol *insn_func(struct instruction *insn)
87 {
88 	struct symbol *sym = insn->sym;
89 
90 	if (sym && sym->type != STT_FUNC)
91 		sym = NULL;
92 
93 	return sym;
94 }
95 
96 #define VISITED_BRANCH		0x01
97 #define VISITED_BRANCH_UACCESS	0x02
98 #define VISITED_BRANCH_MASK	0x03
99 #define VISITED_UNRET		0x04
100 
is_static_jump(struct instruction * insn)101 static inline bool is_static_jump(struct instruction *insn)
102 {
103 	return insn->type == INSN_JUMP_CONDITIONAL ||
104 	       insn->type == INSN_JUMP_UNCONDITIONAL;
105 }
106 
is_dynamic_jump(struct instruction * insn)107 static inline bool is_dynamic_jump(struct instruction *insn)
108 {
109 	return insn->type == INSN_JUMP_DYNAMIC ||
110 	       insn->type == INSN_JUMP_DYNAMIC_CONDITIONAL;
111 }
112 
is_jump(struct instruction * insn)113 static inline bool is_jump(struct instruction *insn)
114 {
115 	return is_static_jump(insn) || is_dynamic_jump(insn);
116 }
117 
118 struct instruction *find_insn(struct objtool_file *file,
119 			      struct section *sec, unsigned long offset);
120 
121 struct instruction *next_insn_same_sec(struct objtool_file *file, struct instruction *insn);
122 
123 #define sec_for_each_insn(file, _sec, insn)				\
124 	for (insn = find_insn(file, _sec, 0);				\
125 	     insn && insn->sec == _sec;					\
126 	     insn = next_insn_same_sec(file, insn))
127 
128 #endif /* _CHECK_H */
129