projects/linux·blob master · refs

119 lines · 2.9 KiB

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
31static const char * const discarded_secs[] = {
32 ".discard",
33 ".exitcall.exit",
34 ".modinfo",
35 ".no_trim_symbol",
36 "__tracepoint_check",
37};
38
39static bool discarded_sec(struct section *sec)
40{
41 if (!(sec->sh.sh_flags & SHF_ALLOC))
42 return true;
43
44 for (int i = 0; i < ARRAY_SIZE(discarded_secs); i++)
45 if (strstarts(sec->name, discarded_secs[i]))
46 return true;
47
48 return false;
49}
50
51static bool symid_needed(struct elf *elf, struct symbol *sym)
52{
53 struct symbol *s;
54
55 if (!is_local_sym(sym) || is_undef_sym(sym))
56 return false;
57
58 if (!is_func_sym(sym) && !is_object_sym(sym))
59 return false;
60
61 if (is_prefix_func(sym))
62 return false;
63
64 if (discarded_sec(sym->sec))
65 return false;
66
67 for_each_sym_by_name(elf, sym->name, s) {
68 if (s == sym || is_sec_sym(s) || is_file_sym(s) || is_undef_sym(s))
69 continue;
70 return true;
71 }
72
73 return false;
74}
75
76int klp_create_symid_sections(struct objtool_file *file)
77{
78 struct elf *elf = file->elf;
79 struct klp_symid *symids;
80 struct section *sec;
81 struct symbol *sym;
82 u64 nr = 0, i = 0;
83
84 if (!str_ends_with(objname, "vmlinux.o"))
85 return 0;
86
87 for_each_sym(elf, sym)
88 if (symid_needed(elf, sym))
89 nr++;
90
91 if (!nr)
92 return 0;
93
94 sec = elf_create_section(elf, KLP_SYMID_SEC, 0, sizeof(struct klp_symid),
95 SHT_PROGBITS, 8, 0);
96 if (!sec)
97 return -1;
98
99 symids = elf_add_data(elf, sec, NULL, nr * sizeof(struct klp_symid));
100 if (!symids)
101 return -1;
102
103 for_each_sym(elf, sym) {
104 if (!symid_needed(elf, sym))
105 continue;
106
107 symids[i].id = bswap_if_needed(elf, i);
108
109 if (!elf_create_reloc(elf, sec,
110 i * sizeof(struct klp_symid) +
111 offsetof(struct klp_symid, addr),
112 sym, 0, R_ABS64))
113 return -1;
114
115 i++;
116 }
117
118 return 0;
119}