xref: /linux/tools/objtool/klp-symid.c (revision 029223d301620bc4e1086696047b0d5d6eba5edd)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Emit the .klp.symid table which allows "objtool klp diff" to reliably
4  * disambiguate duplicate-named local symbols in vmlinux.
5  *
6  * Livepatch identifies a duplicate-named symbol by its position (sympos)
7  * among the same-named kallsyms entries, counted in ascending address order
8  * in the final linked vmlinux.  That order can't be derived from vmlinux.o
9  * alone: the final link reorders sub-sections (.text.unlikely*, .data..*,
10  * etc).
11  *
12  * Bridge the gap with a table which survives the final link: a single
13  * non-alloc section containing an array of { id, addr } entries, where
14  * 'id' is a unique counter identifier and 'addr' has a relocation to the
15  * symbol.  The linker copies 'id' verbatim and resolves 'addr' to the symbol's
16  * final address.
17  *
18  * The table is only emitted for vmlinux.o, and only when klp-build asks for it
19  * with KLP_SYMIDS=1, which adds --klp-symids to the vmlinux.o objtool run.
20  *
21  * It can't survive --gc-sections, which sweeps the whole section; klp-build
22  * rejects CONFIG_LD_DEAD_CODE_DATA_ELIMINATION.
23  */
24 #include <linux/string.h>
25 
26 #include <objtool/objtool.h>
27 #include <objtool/warn.h>
28 #include <objtool/endianness.h>
29 #include <objtool/klp.h>
30 
31 static const char * const discarded_secs[] = {
32 	".discard",
33 	".modinfo",
34 	"__tracepoint_check",
35 };
36 
37 static bool discarded_sec(struct section *sec)
38 {
39 	if (!(sec->sh.sh_flags & SHF_ALLOC))
40 		return true;
41 
42 	for (int i = 0; i < ARRAY_SIZE(discarded_secs); i++)
43 		if (strstarts(sec->name, discarded_secs[i]))
44 			return true;
45 
46 	return false;
47 }
48 
49 static bool symid_needed(struct elf *elf, struct symbol *sym)
50 {
51 	struct symbol *s;
52 
53 	if (!is_local_sym(sym) || is_undef_sym(sym))
54 		return false;
55 
56 	if (!is_func_sym(sym) && !is_object_sym(sym))
57 		return false;
58 
59 	if (is_prefix_func(sym))
60 		return false;
61 
62 	if (discarded_sec(sym->sec))
63 		return false;
64 
65 	for_each_sym_by_name(elf, sym->name, s) {
66 		if (s == sym || is_sec_sym(s) || is_file_sym(s) || is_undef_sym(s))
67 			continue;
68 		return true;
69 	}
70 
71 	return false;
72 }
73 
74 int klp_create_symid_sections(struct objtool_file *file)
75 {
76 	struct elf *elf = file->elf;
77 	struct klp_symid *symids;
78 	struct section *sec;
79 	struct symbol *sym;
80 	u64 nr = 0, i = 0;
81 
82 	if (!str_ends_with(objname, "vmlinux.o"))
83 		return 0;
84 
85 	for_each_sym(elf, sym)
86 		if (symid_needed(elf, sym))
87 			nr++;
88 
89 	if (!nr)
90 		return 0;
91 
92 	sec = elf_create_section(elf, KLP_SYMID_SEC, 0, sizeof(struct klp_symid),
93 				 SHT_PROGBITS, 8, 0);
94 	if (!sec)
95 		return -1;
96 
97 	symids = elf_add_data(elf, sec, NULL, nr * sizeof(struct klp_symid));
98 	if (!symids)
99 		return -1;
100 
101 	for_each_sym(elf, sym) {
102 		if (!symid_needed(elf, sym))
103 			continue;
104 
105 		symids[i].id = bswap_if_needed(elf, i);
106 
107 		if (!elf_create_reloc(elf, sec,
108 				      i * sizeof(struct klp_symid) +
109 				      offsetof(struct klp_symid, addr),
110 				      sym, 0, R_ABS64))
111 			return -1;
112 
113 		i++;
114 	}
115 
116 	return 0;
117 }
118