From fbf50da80b82c2749a76181f6bd76c9ee8d2539c Mon Sep 17 00:00:00 2001 From: Zhipeng Xie Date: Wed, 30 Dec 2020 21:13:10 -0500 Subject: [PATCH 19/37] support remove static variables using KPATCH_IGNORE_STATIC Static variables will be removed due to compiler optimization. And some static variables can be treated as new variables, such as static variables in print limit macros. So add KPATCH_IGNORE_STATIC to tell kpatch to treat the static variables as new variables. Signed-off-by: Zhipeng Xie --- kmod/patch/kpatch-macros.h | 4 +++ kpatch-build/create-diff-object.c | 45 ++++++++++++++++++++++++++++++- 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/kmod/patch/kpatch-macros.h b/kmod/patch/kpatch-macros.h index 8e09702..02d548e 100644 --- a/kmod/patch/kpatch-macros.h +++ b/kmod/patch/kpatch-macros.h @@ -13,6 +13,10 @@ # define __kpatch_section(section) __section(#section) #endif +#define KPATCH_IGNORE_STATIC(_static) \ + char *__UNIQUE_ID(kpatch_ignore_static_) __section(.kpatch.ignore.statics) = _static; + + /* * KPATCH_IGNORE_SECTION macro * diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c index 2510620..c90b30a 100644 --- a/kpatch-build/create-diff-object.c +++ b/kpatch-build/create-diff-object.c @@ -1319,6 +1319,40 @@ static struct rela *kpatch_find_static_twin_ref(struct section *relasec, return NULL; } +static int kpatch_mark_ignored_statics(struct kpatch_elf *kelf, struct symbol *sym) +{ + struct section *sec, *strsec; + struct rela *rela; + char *name; + + sec = find_section_by_name(&kelf->sections, ".kpatch.ignore.statics"); + if (!sec) + return 0; + + list_for_each_entry(rela, &sec->rela->relas, list) { + strsec = rela->sym->sec; + strsec->status = CHANGED; + /* + * Include the string section here. This is because the + * KPATCH_IGNORE_STATIC() macro is passed a literal string + * by the patch author, resulting in a change to the string + * section. If we don't include it, then we will potentially + * get a "changed section not included" error in + * kpatch_verify_patchability() if no other function based change + * also changes the string section. We could try to exclude each + * literal string added to the section by KPATCH_IGNORE_STATIC() + * from the section data comparison, but this is a simpler way. + */ + strsec->include = 1; + strsec->secsym->include = 1; + name = strsec->data->d_buf + rela->addend; + if (!strncmp(name, sym->name, strlen(name))) + return 1; + } + + return 0; +} + /* * gcc renames static local variables by appending a period and a number. For * example, __foo could be renamed to __foo.31452. Unfortunately this number @@ -1399,6 +1433,11 @@ static void kpatch_correlate_static_local_variables(struct kpatch_elf *orig, if (sym->twin) continue; + if (kpatch_mark_ignored_statics(patched, sym)) { + log_normal("KPATCH_IGNORE_STATIC:ignore static variable %s\n", sym->name); + continue; + } + bundled = sym == sym->sec->sym; if (bundled && sym->sec == relasec->base) { /* @@ -1456,6 +1495,11 @@ static void kpatch_correlate_static_local_variables(struct kpatch_elf *orig, if (!kpatch_is_normal_static_local(sym)) continue; + if (kpatch_mark_ignored_statics(patched, sym)) { + log_normal("KPATCH_IGNORE_STATIC:ignore static variable %s\n", sym->name); + continue; + } + if (!relasec->twin && relasec->base->sym) { struct symbol *parent = NULL; @@ -1499,7 +1543,6 @@ static void kpatch_correlate_static_local_variables(struct kpatch_elf *orig, log_normal("WARNING: unable to correlate static local variable %s used by %s, assuming variable is new\n", sym->name, kpatch_section_function_name(relasec)); - return; } } } -- 2.33.0