objtool/klp: Add post-link subcommand to finalize livepatch modules
Livepatch needs some ELF magic which linkers don't like:
- Two relocation sections (.rela*, .klp.rela*) for the same text
section.
- Use of SHN_LIVEPATCH to mark livepatch symbols.
Unfortunately linkers tend to mangle such things. To work around that,
klp diff generates a linker-compliant intermediate binary which encodes
the relevant KLP section/reloc/symbol metadata.
After module linking, the .ko then needs to be converted to an actual
livepatch module. Introduce a new klp post-link subcommand to do so.
Acked-by: Petr Mladek <pmladek@suse.com>
Tested-by: Joe Lawrence <joe.lawrence@redhat.com>
Signed-off-by: Josh Poimboeuf <jpoimboe@kernel.org>
pull/1354/merge
parent
7c2575a640
commit
ebe864b553
|
|
@ -9,7 +9,7 @@ objtool-y += elf.o
|
||||||
objtool-y += objtool.o
|
objtool-y += objtool.o
|
||||||
|
|
||||||
objtool-$(BUILD_ORC) += orc_gen.o orc_dump.o
|
objtool-$(BUILD_ORC) += orc_gen.o orc_dump.o
|
||||||
objtool-$(BUILD_KLP) += builtin-klp.o klp-diff.o
|
objtool-$(BUILD_KLP) += builtin-klp.o klp-diff.o klp-post-link.o
|
||||||
|
|
||||||
objtool-y += libstring.o
|
objtool-y += libstring.o
|
||||||
objtool-y += libctype.o
|
objtool-y += libctype.o
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ struct subcmd {
|
||||||
|
|
||||||
static struct subcmd subcmds[] = {
|
static struct subcmd subcmds[] = {
|
||||||
{ "diff", "Generate binary diff of two object files", cmd_klp_diff, },
|
{ "diff", "Generate binary diff of two object files", cmd_klp_diff, },
|
||||||
|
{ "post-link", "Finalize klp symbols/relocs after module linking", cmd_klp_post_link, },
|
||||||
};
|
};
|
||||||
|
|
||||||
static void cmd_klp_usage(void)
|
static void cmd_klp_usage(void)
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,9 @@
|
||||||
#ifndef _OBJTOOL_KLP_H
|
#ifndef _OBJTOOL_KLP_H
|
||||||
#define _OBJTOOL_KLP_H
|
#define _OBJTOOL_KLP_H
|
||||||
|
|
||||||
|
#define SHF_RELA_LIVEPATCH 0x00100000
|
||||||
|
#define SHN_LIVEPATCH 0xff20
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* __klp_objects and __klp_funcs are created by klp diff and used by the patch
|
* __klp_objects and __klp_funcs are created by klp diff and used by the patch
|
||||||
* module init code to build the klp_patch, klp_object and klp_func structs
|
* module init code to build the klp_patch, klp_object and klp_func structs
|
||||||
|
|
@ -27,5 +30,6 @@ struct klp_reloc {
|
||||||
};
|
};
|
||||||
|
|
||||||
int cmd_klp_diff(int argc, const char **argv);
|
int cmd_klp_diff(int argc, const char **argv);
|
||||||
|
int cmd_klp_post_link(int argc, const char **argv);
|
||||||
|
|
||||||
#endif /* _OBJTOOL_KLP_H */
|
#endif /* _OBJTOOL_KLP_H */
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,168 @@
|
||||||
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
||||||
|
/*
|
||||||
|
* Read the intermediate KLP reloc/symbol representations created by klp diff
|
||||||
|
* and convert them to the proper format required by livepatch. This needs to
|
||||||
|
* run last to avoid linker wreckage. Linkers don't tend to handle the "two
|
||||||
|
* rela sections for a single base section" case very well, nor do they like
|
||||||
|
* SHN_LIVEPATCH.
|
||||||
|
*
|
||||||
|
* This is the final tool in the livepatch module generation pipeline:
|
||||||
|
*
|
||||||
|
* kernel builds -> objtool klp diff -> module link -> objtool klp post-link
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <gelf.h>
|
||||||
|
#include <objtool/objtool.h>
|
||||||
|
#include <objtool/warn.h>
|
||||||
|
#include <objtool/klp.h>
|
||||||
|
#include <objtool/util.h>
|
||||||
|
#include <linux/livepatch_external.h>
|
||||||
|
|
||||||
|
static int fix_klp_relocs(struct elf *elf)
|
||||||
|
{
|
||||||
|
struct section *symtab, *klp_relocs;
|
||||||
|
|
||||||
|
klp_relocs = find_section_by_name(elf, KLP_RELOCS_SEC);
|
||||||
|
if (!klp_relocs)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
symtab = find_section_by_name(elf, ".symtab");
|
||||||
|
if (!symtab) {
|
||||||
|
ERROR("missing .symtab");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < sec_size(klp_relocs) / sizeof(struct klp_reloc); i++) {
|
||||||
|
struct klp_reloc *klp_reloc;
|
||||||
|
unsigned long klp_reloc_off;
|
||||||
|
struct section *sec, *tmp, *klp_rsec;
|
||||||
|
unsigned long offset;
|
||||||
|
struct reloc *reloc;
|
||||||
|
char sym_modname[64];
|
||||||
|
char rsec_name[SEC_NAME_LEN];
|
||||||
|
u64 addend;
|
||||||
|
struct symbol *sym, *klp_sym;
|
||||||
|
|
||||||
|
klp_reloc_off = i * sizeof(*klp_reloc);
|
||||||
|
klp_reloc = klp_relocs->data->d_buf + klp_reloc_off;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Read __klp_relocs[i]:
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* klp_reloc.sec_offset */
|
||||||
|
reloc = find_reloc_by_dest(elf, klp_relocs,
|
||||||
|
klp_reloc_off + offsetof(struct klp_reloc, offset));
|
||||||
|
if (!reloc) {
|
||||||
|
ERROR("malformed " KLP_RELOCS_SEC " section");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
sec = reloc->sym->sec;
|
||||||
|
offset = reloc_addend(reloc);
|
||||||
|
|
||||||
|
/* klp_reloc.sym */
|
||||||
|
reloc = find_reloc_by_dest(elf, klp_relocs,
|
||||||
|
klp_reloc_off + offsetof(struct klp_reloc, sym));
|
||||||
|
if (!reloc) {
|
||||||
|
ERROR("malformed " KLP_RELOCS_SEC " section");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
klp_sym = reloc->sym;
|
||||||
|
addend = reloc_addend(reloc);
|
||||||
|
|
||||||
|
/* symbol format: .klp.sym.modname.sym_name,sympos */
|
||||||
|
if (sscanf(klp_sym->name + strlen(KLP_SYM_PREFIX), "%55[^.]", sym_modname) != 1)
|
||||||
|
ERROR("can't find modname in klp symbol '%s'", klp_sym->name);
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Create the KLP rela:
|
||||||
|
*/
|
||||||
|
|
||||||
|
/* section format: .klp.rela.sec_objname.section_name */
|
||||||
|
if (snprintf_check(rsec_name, SEC_NAME_LEN,
|
||||||
|
KLP_RELOC_SEC_PREFIX "%s.%s",
|
||||||
|
sym_modname, sec->name))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
klp_rsec = find_section_by_name(elf, rsec_name);
|
||||||
|
if (!klp_rsec) {
|
||||||
|
klp_rsec = elf_create_section(elf, rsec_name, 0,
|
||||||
|
elf_rela_size(elf),
|
||||||
|
SHT_RELA, elf_addr_size(elf),
|
||||||
|
SHF_ALLOC | SHF_INFO_LINK | SHF_RELA_LIVEPATCH);
|
||||||
|
if (!klp_rsec)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
klp_rsec->sh.sh_link = symtab->idx;
|
||||||
|
klp_rsec->sh.sh_info = sec->idx;
|
||||||
|
klp_rsec->base = sec;
|
||||||
|
}
|
||||||
|
|
||||||
|
tmp = sec->rsec;
|
||||||
|
sec->rsec = klp_rsec;
|
||||||
|
if (!elf_create_reloc(elf, sec, offset, klp_sym, addend, klp_reloc->type))
|
||||||
|
return -1;
|
||||||
|
sec->rsec = tmp;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Fix up the corresponding KLP symbol:
|
||||||
|
*/
|
||||||
|
|
||||||
|
klp_sym->sym.st_shndx = SHN_LIVEPATCH;
|
||||||
|
if (!gelf_update_sym(symtab->data, klp_sym->idx, &klp_sym->sym)) {
|
||||||
|
ERROR_ELF("gelf_update_sym");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Disable the original non-KLP reloc by converting it to R_*_NONE:
|
||||||
|
*/
|
||||||
|
|
||||||
|
reloc = find_reloc_by_dest(elf, sec, offset);
|
||||||
|
sym = reloc->sym;
|
||||||
|
sym->sym.st_shndx = SHN_LIVEPATCH;
|
||||||
|
set_reloc_type(elf, reloc, 0);
|
||||||
|
if (!gelf_update_sym(symtab->data, sym->idx, &sym->sym)) {
|
||||||
|
ERROR_ELF("gelf_update_sym");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This runs on the livepatch module after all other linking has been done. It
|
||||||
|
* converts the intermediate __klp_relocs section into proper KLP relocs to be
|
||||||
|
* processed by livepatch. This needs to run last to avoid linker wreckage.
|
||||||
|
* Linkers don't tend to handle the "two rela sections for a single base
|
||||||
|
* section" case very well, nor do they appreciate SHN_LIVEPATCH.
|
||||||
|
*/
|
||||||
|
int cmd_klp_post_link(int argc, const char **argv)
|
||||||
|
{
|
||||||
|
struct elf *elf;
|
||||||
|
|
||||||
|
argc--;
|
||||||
|
argv++;
|
||||||
|
|
||||||
|
if (argc != 1) {
|
||||||
|
fprintf(stderr, "%d\n", argc);
|
||||||
|
fprintf(stderr, "usage: objtool link <file.ko>\n");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
elf = elf_open_read(argv[0], O_RDWR);
|
||||||
|
if (!elf)
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (fix_klp_relocs(elf))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
if (elf_write(elf))
|
||||||
|
return -1;
|
||||||
|
|
||||||
|
return elf_close(elf);
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue