From 1270a6e969b0584f8fb87fad7ee9227852a0c6c5 Mon Sep 17 00:00:00 2001 From: Joe Lawrence Date: Wed, 2 Feb 2022 15:43:15 -0500 Subject: [PATCH] squash --- kpatch-build/create-diff-object.c | 221 +++++++++++++++--------------- kpatch-build/kpatch-elf.c | 30 ++-- kpatch-build/kpatch-elf.h | 2 +- 3 files changed, 129 insertions(+), 124 deletions(-) diff --git a/kpatch-build/create-diff-object.c b/kpatch-build/create-diff-object.c index af3f663fe213..21aa4f254f7d 100644 --- a/kpatch-build/create-diff-object.c +++ b/kpatch-build/create-diff-object.c @@ -158,12 +158,17 @@ static int is_bundleable(struct symbol *sym) static bool is_gcc6_localentry_bundled_sym(struct kpatch_elf *kelf, struct symbol *sym) { - if (kelf->target_arch == PPC64) { + switch(kelf->arch) { + case PPC64: return ((PPC64_LOCAL_ENTRY_OFFSET(sym->sym.st_other) != 0) && sym->sym.st_value == 8); + case X86_64: + return false; + default: + ERROR("unsupported arch"); } - return 0; + return false; } /* @@ -754,13 +759,13 @@ static bool kpatch_line_macro_change_only_ppc64le(struct section *sec) static bool kpatch_line_macro_change_only(struct kpatch_elf *kelf, struct section *sec) { - switch(kelf->target_arch) { - case PPC64: - return kpatch_line_macro_change_only_ppc64le(sec); - case X86_64: - return kpatch_line_macro_change_only_x86_64(sec); - default: - ERROR("unsupported arch"); + switch(kelf->arch) { + case PPC64: + return kpatch_line_macro_change_only_ppc64le(sec); + case X86_64: + return kpatch_line_macro_change_only_x86_64(sec); + default: + ERROR("unsupported arch"); } return false; } @@ -1540,26 +1545,26 @@ static void kpatch_replace_sections_syms(struct kpatch_elf *kelf) continue; } - switch(kelf->target_arch) { - case PPC64: + switch(kelf->arch) { + case PPC64: + add_off = 0; + break; + case X86_64: + if (rela->type == R_X86_64_PC32 || + rela->type == R_X86_64_PLT32) { + struct insn insn; + rela_insn(sec, rela, &insn); + add_off = (unsigned int)((long)insn.next_byte - + (long)sec->base->data->d_buf - + rela->offset); + } else if (rela->type == R_X86_64_64 || + rela->type == R_X86_64_32S) add_off = 0; - break; - case X86_64: - if (rela->type == R_X86_64_PC32 || - rela->type == R_X86_64_PLT32) { - struct insn insn; - rela_insn(sec, rela, &insn); - add_off = (unsigned int)((long)insn.next_byte - - (long)sec->base->data->d_buf - - rela->offset); - } else if (rela->type == R_X86_64_64 || - rela->type == R_X86_64_32S) - add_off = 0; - else - continue; - break; - default: - ERROR("unsupported arch"); + else + continue; + break; + default: + ERROR("unsupported arch"); } /* @@ -1926,7 +1931,7 @@ static void kpatch_migrate_included_elements(struct kpatch_elf *kelf, struct kpa if (!out) ERROR("malloc"); memset(out, 0, sizeof(*out)); - out->target_arch = kelf->target_arch; + out->arch = kelf->arch; INIT_LIST_HEAD(&out->sections); INIT_LIST_HEAD(&out->symbols); INIT_LIST_HEAD(&out->strings); @@ -2784,7 +2789,7 @@ static void kpatch_create_kpatch_arch_section(struct kpatch_elf *kelf, char *obj ERROR("can't find .kpatch.strings symbol"); for (special = special_sections; special->name; special++) { - if ((special->arch & kelf->target_arch) == 0) + if ((special->arch & kelf->arch) == 0) continue; if (strcmp(special->name, ".parainstructions") && @@ -2827,7 +2832,7 @@ static void kpatch_process_special_sections(struct kpatch_elf *kelf, int altinstr = 0; for (special = special_sections; special->name; special++) { - if ((special->arch & kelf->target_arch) == 0) + if ((special->arch & kelf->arch) == 0) continue; sec = find_section_by_name(&kelf->sections, special->name); @@ -3267,7 +3272,7 @@ static void kpatch_create_intermediate_sections(struct kpatch_elf *kelf, special = false; for (s = special_sections; s->name; s++) { - if ((s->arch & kelf->target_arch) == 0) + if ((s->arch & kelf->arch) == 0) continue; if (!strcmp(sec->base->name, s->name)) @@ -3474,69 +3479,69 @@ static void kpatch_create_mcount_sections(struct kpatch_elf *kelf) continue; } - switch(kelf->target_arch) { - case PPC64: { - bool found = false; + switch(kelf->arch) { + case PPC64: { + bool found = false; - list_for_each_entry(rela, &sym->sec->rela->relas, list) - if (!strcmp(rela->sym->name, "_mcount")) { - found = true; - break; - } + list_for_each_entry(rela, &sym->sec->rela->relas, list) + if (!strcmp(rela->sym->name, "_mcount")) { + found = true; + break; + } - if (!found) - ERROR("%s: unexpected missing call to _mcount()", __func__); + if (!found) + ERROR("%s: unexpected missing call to _mcount()", __func__); - insn_offset = rela->offset; - break; - } - case X86_64: { - unsigned char *insn; - void *newdata; + insn_offset = rela->offset; + break; + } + case X86_64: { + unsigned char *insn; + void *newdata; - rela = list_first_entry(&sym->sec->rela->relas, struct rela, list); + rela = list_first_entry(&sym->sec->rela->relas, struct rela, list); - /* - * For "call fentry", the relocation points to 1 byte past the - * beginning of the instruction. - */ - insn_offset = rela->offset - 1; + /* + * For "call fentry", the relocation points to 1 byte past the + * beginning of the instruction. + */ + insn_offset = rela->offset - 1; - /* - * R_X86_64_NONE is only generated by older versions of - * kernel/gcc which use the mcount script. There's a - * NOP instead of a call to fentry. - */ - if (rela->type != R_X86_64_NONE) - break; + /* + * R_X86_64_NONE is only generated by older versions of + * kernel/gcc which use the mcount script. There's a + * NOP instead of a call to fentry. + */ + if (rela->type != R_X86_64_NONE) + break; - /* Make a writable copy of the text section data */ - newdata = malloc(sym->sec->data->d_size); - if (!newdata) - ERROR("malloc"); - memcpy(newdata, sym->sec->data->d_buf, sym->sec->data->d_size); - sym->sec->data->d_buf = newdata; - insn = newdata; + /* Make a writable copy of the text section data */ + newdata = malloc(sym->sec->data->d_size); + if (!newdata) + ERROR("malloc"); + memcpy(newdata, sym->sec->data->d_buf, sym->sec->data->d_size); + sym->sec->data->d_buf = newdata; + insn = newdata; - /* - * Replace the NOP with a call to fentry. The fentry - * rela symbol is already there, just need to change - * the relocation type accordingly. - */ - insn = sym->sec->data->d_buf; - if (insn[0] != 0xf) - ERROR("%s: unexpected instruction at the start of the function", sym->name); - insn[0] = 0xe8; - insn[1] = 0; - insn[2] = 0; - insn[3] = 0; - insn[4] = 0; - - rela->type = R_X86_64_PC32; - break; - } - default: - ERROR("unsupported arch"); + /* + * Replace the NOP with a call to fentry. The fentry + * rela symbol is already there, just need to change + * the relocation type accordingly. + */ + insn = sym->sec->data->d_buf; + if (insn[0] != 0xf) + ERROR("%s: unexpected instruction at the start of the function", sym->name); + insn[0] = 0xe8; + insn[1] = 0; + insn[2] = 0; + insn[3] = 0; + insn[4] = 0; + + rela->type = R_X86_64_PC32; + break; + } + default: + ERROR("unsupported arch"); } /* @@ -3657,7 +3662,7 @@ static void kpatch_no_sibling_calls_ppc64le(struct kpatch_elf *kelf) unsigned int insn; unsigned int offset; - if (kelf->target_arch != PPC64) + if (kelf->arch != PPC64) return; list_for_each_entry(sym, &kelf->symbols, list) { @@ -3703,28 +3708,28 @@ static void kpatch_find_func_profiling_calls(struct kpatch_elf *kelf) if (sym->type != STT_FUNC || !sym->sec || !sym->sec->rela) continue; - switch(kelf->target_arch) { - case PPC64: - list_for_each_entry(rela, &sym->sec->rela->relas, list) { - if (!strcmp(rela->sym->name, "_mcount")) { - sym->has_func_profiling = 1; - break; - } + switch(kelf->arch) { + case PPC64: + list_for_each_entry(rela, &sym->sec->rela->relas, list) { + if (!strcmp(rela->sym->name, "_mcount")) { + sym->has_func_profiling = 1; + break; } - break; - case X86_64: - rela = list_first_entry(&sym->sec->rela->relas, struct rela, - list); - if ((rela->type != R_X86_64_NONE && - rela->type != R_X86_64_PC32 && - rela->type != R_X86_64_PLT32) || - strcmp(rela->sym->name, "__fentry__")) - continue; + } + break; + case X86_64: + rela = list_first_entry(&sym->sec->rela->relas, struct rela, + list); + if ((rela->type != R_X86_64_NONE && + rela->type != R_X86_64_PC32 && + rela->type != R_X86_64_PLT32) || + strcmp(rela->sym->name, "__fentry__")) + continue; - sym->has_func_profiling = 1; - break; - default: - ERROR("unsupported arch"); + sym->has_func_profiling = 1; + break; + default: + ERROR("unsupported arch"); } } } diff --git a/kpatch-build/kpatch-elf.c b/kpatch-build/kpatch-elf.c index 596d1a58a14d..b9d46a3a6bd2 100644 --- a/kpatch-build/kpatch-elf.c +++ b/kpatch-build/kpatch-elf.c @@ -134,13 +134,13 @@ struct rela *find_rela_by_offset(struct section *relasec, unsigned int offset) unsigned int absolute_rela_type(struct kpatch_elf *kelf) { - switch(kelf->target_arch) { - case PPC64: - return R_PPC64_ADDR64; - case X86_64: - return R_X86_64_64; - default: - ERROR("unsupported arch"); + switch(kelf->arch) { + case PPC64: + return R_PPC64_ADDR64; + case X86_64: + return R_X86_64_64; + default: + ERROR("unsupported arch"); } return 0; } @@ -371,14 +371,14 @@ struct kpatch_elf *kpatch_elf_open(const char *name) if (!gelf_getehdr(kelf->elf, &ehdr)) ERROR("gelf_getehdr"); switch (ehdr.e_machine) { - case EM_PPC64: - kelf->target_arch = PPC64; - break; - case EM_X86_64: - kelf->target_arch = X86_64; - break; - default: - ERROR("Unsupported target architecture"); + case EM_PPC64: + kelf->arch = PPC64; + break; + case EM_X86_64: + kelf->arch = X86_64; + break; + default: + ERROR("Unsupported target architecture"); } return kelf; } diff --git a/kpatch-build/kpatch-elf.h b/kpatch-build/kpatch-elf.h index cfd256a1241b..b3cec43ed310 100644 --- a/kpatch-build/kpatch-elf.h +++ b/kpatch-build/kpatch-elf.h @@ -116,7 +116,7 @@ enum architecture { struct kpatch_elf { Elf *elf; - enum architecture target_arch; + enum architecture arch; struct list_head sections; struct list_head symbols; struct list_head strings; -- 2.26.3