xref: /linux/tools/objtool/include/objtool/checksum.h (revision a87bbc4578fd686d535fbd62e8bc73fc6c7c5415)
1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 #ifndef _OBJTOOL_CHECKSUM_H
3 #define _OBJTOOL_CHECKSUM_H
4 
5 #include <objtool/elf.h>
6 
7 #ifdef BUILD_KLP
8 
9 static inline void checksum_init(struct symbol *sym)
10 {
11 	if (sym && !sym->csum.state) {
12 		sym->csum.state = XXH3_createState();
13 		XXH3_64bits_reset(sym->csum.state);
14 	}
15 }
16 
17 static inline void __checksum_update(struct symbol *sym, const void *data,
18 				     size_t size)
19 {
20 	XXH3_64bits_update(sym->csum.state, data, size);
21 }
22 
23 static inline void __checksum_update_insn(struct symbol *sym,
24 					  struct instruction *insn,
25 					  const void *data, size_t size)
26 {
27 	__checksum_update(sym, data, size);
28 	dbg_checksum_insn(sym, insn, XXH3_64bits_digest(sym->csum.state));
29 }
30 
31 static inline void __checksum_update_object(struct symbol *sym,
32 					    unsigned long offset,
33 					    const char *what, const void *data,
34 					    size_t size)
35 {
36 	__checksum_update(sym, &offset, sizeof(offset));
37 	__checksum_update(sym, data, size);
38 	dbg_checksum_object(sym, offset, what, XXH3_64bits_digest(sym->csum.state));
39 }
40 
41 static inline void checksum_finish(struct symbol *sym)
42 {
43 	if (sym && sym->csum.state) {
44 		sym->csum.checksum = XXH3_64bits_digest(sym->csum.state);
45 		XXH3_freeState(sym->csum.state);
46 		sym->csum.state = NULL;
47 	}
48 }
49 
50 int calculate_checksums(struct objtool_file *file);
51 int create_sym_checksum_section(struct objtool_file *file);
52 
53 #else /* !BUILD_KLP */
54 
55 static inline int calculate_checksums(struct objtool_file *file) { return -ENOSYS; }
56 static inline int create_sym_checksum_section(struct objtool_file *file) { return -EINVAL; }
57 
58 #endif /* !BUILD_KLP */
59 
60 #endif /* _OBJTOOL_CHECKSUM_H */
61