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