mirror of
https://github.com/torvalds/linux
synced 2024-09-16 00:58:07 +00:00
objtool: Refactor ORC section generation
Decouple ORC entries from instructions. This simplifies the control/data flow, and is going to make it easier to support alternative instructions which change the stack layout. Signed-off-by: Josh Poimboeuf <jpoimboe@redhat.com>
This commit is contained in:
parent
5ed934e57e
commit
ab4e0744e9
|
@ -47,10 +47,6 @@ ifeq ($(SRCARCH),x86)
|
||||||
SUBCMD_ORC := y
|
SUBCMD_ORC := y
|
||||||
endif
|
endif
|
||||||
|
|
||||||
ifeq ($(SUBCMD_ORC),y)
|
|
||||||
CFLAGS += -DINSN_USE_ORC
|
|
||||||
endif
|
|
||||||
|
|
||||||
export SUBCMD_CHECK SUBCMD_ORC
|
export SUBCMD_CHECK SUBCMD_ORC
|
||||||
export srctree OUTPUT CFLAGS SRCARCH AWK
|
export srctree OUTPUT CFLAGS SRCARCH AWK
|
||||||
include $(srctree)/tools/build/Makefile.include
|
include $(srctree)/tools/build/Makefile.include
|
||||||
|
|
|
@ -51,11 +51,7 @@ int cmd_orc(int argc, const char **argv)
|
||||||
if (list_empty(&file->insn_list))
|
if (list_empty(&file->insn_list))
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
ret = create_orc(file);
|
ret = orc_create(file);
|
||||||
if (ret)
|
|
||||||
return ret;
|
|
||||||
|
|
||||||
ret = create_orc_sections(file);
|
|
||||||
if (ret)
|
if (ret)
|
||||||
return ret;
|
return ret;
|
||||||
|
|
||||||
|
|
|
@ -11,10 +11,6 @@
|
||||||
#include <objtool/objtool.h>
|
#include <objtool/objtool.h>
|
||||||
#include <objtool/cfi.h>
|
#include <objtool/cfi.h>
|
||||||
|
|
||||||
#ifdef INSN_USE_ORC
|
|
||||||
#include <asm/orc_types.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum insn_type {
|
enum insn_type {
|
||||||
INSN_JUMP_CONDITIONAL,
|
INSN_JUMP_CONDITIONAL,
|
||||||
INSN_JUMP_UNCONDITIONAL,
|
INSN_JUMP_UNCONDITIONAL,
|
||||||
|
|
|
@ -43,9 +43,6 @@ struct instruction {
|
||||||
struct symbol *func;
|
struct symbol *func;
|
||||||
struct list_head stack_ops;
|
struct list_head stack_ops;
|
||||||
struct cfi_state cfi;
|
struct cfi_state cfi;
|
||||||
#ifdef INSN_USE_ORC
|
|
||||||
struct orc_entry orc;
|
|
||||||
#endif
|
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline bool is_static_jump(struct instruction *insn)
|
static inline bool is_static_jump(struct instruction *insn)
|
||||||
|
|
|
@ -26,7 +26,6 @@ struct objtool_file *objtool_open_read(const char *_objname);
|
||||||
|
|
||||||
int check(struct objtool_file *file);
|
int check(struct objtool_file *file);
|
||||||
int orc_dump(const char *objname);
|
int orc_dump(const char *objname);
|
||||||
int create_orc(struct objtool_file *file);
|
int orc_create(struct objtool_file *file);
|
||||||
int create_orc_sections(struct objtool_file *file);
|
|
||||||
|
|
||||||
#endif /* _OBJTOOL_H */
|
#endif /* _OBJTOOL_H */
|
||||||
|
|
|
@ -13,89 +13,84 @@
|
||||||
#include <objtool/warn.h>
|
#include <objtool/warn.h>
|
||||||
#include <objtool/endianness.h>
|
#include <objtool/endianness.h>
|
||||||
|
|
||||||
int create_orc(struct objtool_file *file)
|
static int init_orc_entry(struct orc_entry *orc, struct cfi_state *cfi)
|
||||||
{
|
{
|
||||||
struct instruction *insn;
|
struct instruction *insn = container_of(cfi, struct instruction, cfi);
|
||||||
|
struct cfi_reg *bp = &cfi->regs[CFI_BP];
|
||||||
|
|
||||||
for_each_insn(file, insn) {
|
memset(orc, 0, sizeof(*orc));
|
||||||
struct orc_entry *orc = &insn->orc;
|
|
||||||
struct cfi_reg *cfa = &insn->cfi.cfa;
|
|
||||||
struct cfi_reg *bp = &insn->cfi.regs[CFI_BP];
|
|
||||||
|
|
||||||
if (!insn->sec->text)
|
orc->end = cfi->end;
|
||||||
continue;
|
|
||||||
|
|
||||||
orc->end = insn->cfi.end;
|
if (cfi->cfa.base == CFI_UNDEFINED) {
|
||||||
|
orc->sp_reg = ORC_REG_UNDEFINED;
|
||||||
if (cfa->base == CFI_UNDEFINED) {
|
return 0;
|
||||||
orc->sp_reg = ORC_REG_UNDEFINED;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (cfa->base) {
|
|
||||||
case CFI_SP:
|
|
||||||
orc->sp_reg = ORC_REG_SP;
|
|
||||||
break;
|
|
||||||
case CFI_SP_INDIRECT:
|
|
||||||
orc->sp_reg = ORC_REG_SP_INDIRECT;
|
|
||||||
break;
|
|
||||||
case CFI_BP:
|
|
||||||
orc->sp_reg = ORC_REG_BP;
|
|
||||||
break;
|
|
||||||
case CFI_BP_INDIRECT:
|
|
||||||
orc->sp_reg = ORC_REG_BP_INDIRECT;
|
|
||||||
break;
|
|
||||||
case CFI_R10:
|
|
||||||
orc->sp_reg = ORC_REG_R10;
|
|
||||||
break;
|
|
||||||
case CFI_R13:
|
|
||||||
orc->sp_reg = ORC_REG_R13;
|
|
||||||
break;
|
|
||||||
case CFI_DI:
|
|
||||||
orc->sp_reg = ORC_REG_DI;
|
|
||||||
break;
|
|
||||||
case CFI_DX:
|
|
||||||
orc->sp_reg = ORC_REG_DX;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
WARN_FUNC("unknown CFA base reg %d",
|
|
||||||
insn->sec, insn->offset, cfa->base);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch(bp->base) {
|
|
||||||
case CFI_UNDEFINED:
|
|
||||||
orc->bp_reg = ORC_REG_UNDEFINED;
|
|
||||||
break;
|
|
||||||
case CFI_CFA:
|
|
||||||
orc->bp_reg = ORC_REG_PREV_SP;
|
|
||||||
break;
|
|
||||||
case CFI_BP:
|
|
||||||
orc->bp_reg = ORC_REG_BP;
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
WARN_FUNC("unknown BP base reg %d",
|
|
||||||
insn->sec, insn->offset, bp->base);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
orc->sp_offset = cfa->offset;
|
|
||||||
orc->bp_offset = bp->offset;
|
|
||||||
orc->type = insn->cfi.type;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
switch (cfi->cfa.base) {
|
||||||
|
case CFI_SP:
|
||||||
|
orc->sp_reg = ORC_REG_SP;
|
||||||
|
break;
|
||||||
|
case CFI_SP_INDIRECT:
|
||||||
|
orc->sp_reg = ORC_REG_SP_INDIRECT;
|
||||||
|
break;
|
||||||
|
case CFI_BP:
|
||||||
|
orc->sp_reg = ORC_REG_BP;
|
||||||
|
break;
|
||||||
|
case CFI_BP_INDIRECT:
|
||||||
|
orc->sp_reg = ORC_REG_BP_INDIRECT;
|
||||||
|
break;
|
||||||
|
case CFI_R10:
|
||||||
|
orc->sp_reg = ORC_REG_R10;
|
||||||
|
break;
|
||||||
|
case CFI_R13:
|
||||||
|
orc->sp_reg = ORC_REG_R13;
|
||||||
|
break;
|
||||||
|
case CFI_DI:
|
||||||
|
orc->sp_reg = ORC_REG_DI;
|
||||||
|
break;
|
||||||
|
case CFI_DX:
|
||||||
|
orc->sp_reg = ORC_REG_DX;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
WARN_FUNC("unknown CFA base reg %d",
|
||||||
|
insn->sec, insn->offset, cfi->cfa.base);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (bp->base) {
|
||||||
|
case CFI_UNDEFINED:
|
||||||
|
orc->bp_reg = ORC_REG_UNDEFINED;
|
||||||
|
break;
|
||||||
|
case CFI_CFA:
|
||||||
|
orc->bp_reg = ORC_REG_PREV_SP;
|
||||||
|
break;
|
||||||
|
case CFI_BP:
|
||||||
|
orc->bp_reg = ORC_REG_BP;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
WARN_FUNC("unknown BP base reg %d",
|
||||||
|
insn->sec, insn->offset, bp->base);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
orc->sp_offset = cfi->cfa.offset;
|
||||||
|
orc->bp_offset = bp->offset;
|
||||||
|
orc->type = cfi->type;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int create_orc_entry(struct elf *elf, struct section *u_sec, struct section *ip_relocsec,
|
static int write_orc_entry(struct elf *elf, struct section *orc_sec,
|
||||||
unsigned int idx, struct section *insn_sec,
|
struct section *ip_rsec, unsigned int idx,
|
||||||
unsigned long insn_off, struct orc_entry *o)
|
struct section *insn_sec, unsigned long insn_off,
|
||||||
|
struct orc_entry *o)
|
||||||
{
|
{
|
||||||
struct orc_entry *orc;
|
struct orc_entry *orc;
|
||||||
struct reloc *reloc;
|
struct reloc *reloc;
|
||||||
|
|
||||||
/* populate ORC data */
|
/* populate ORC data */
|
||||||
orc = (struct orc_entry *)u_sec->data->d_buf + idx;
|
orc = (struct orc_entry *)orc_sec->data->d_buf + idx;
|
||||||
memcpy(orc, o, sizeof(*orc));
|
memcpy(orc, o, sizeof(*orc));
|
||||||
orc->sp_offset = bswap_if_needed(orc->sp_offset);
|
orc->sp_offset = bswap_if_needed(orc->sp_offset);
|
||||||
orc->bp_offset = bswap_if_needed(orc->bp_offset);
|
orc->bp_offset = bswap_if_needed(orc->bp_offset);
|
||||||
|
@ -117,102 +112,109 @@ static int create_orc_entry(struct elf *elf, struct section *u_sec, struct secti
|
||||||
|
|
||||||
reloc->type = R_X86_64_PC32;
|
reloc->type = R_X86_64_PC32;
|
||||||
reloc->offset = idx * sizeof(int);
|
reloc->offset = idx * sizeof(int);
|
||||||
reloc->sec = ip_relocsec;
|
reloc->sec = ip_rsec;
|
||||||
|
|
||||||
elf_add_reloc(elf, reloc);
|
elf_add_reloc(elf, reloc);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int create_orc_sections(struct objtool_file *file)
|
struct orc_list_entry {
|
||||||
{
|
struct list_head list;
|
||||||
struct instruction *insn, *prev_insn;
|
struct orc_entry orc;
|
||||||
struct section *sec, *u_sec, *ip_relocsec;
|
struct section *insn_sec;
|
||||||
unsigned int idx;
|
unsigned long insn_off;
|
||||||
|
};
|
||||||
|
|
||||||
struct orc_entry empty = {
|
static int orc_list_add(struct list_head *orc_list, struct orc_entry *orc,
|
||||||
.sp_reg = ORC_REG_UNDEFINED,
|
struct section *sec, unsigned long offset)
|
||||||
|
{
|
||||||
|
struct orc_list_entry *entry = malloc(sizeof(*entry));
|
||||||
|
|
||||||
|
if (!entry) {
|
||||||
|
WARN("malloc failed");
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
entry->orc = *orc;
|
||||||
|
entry->insn_sec = sec;
|
||||||
|
entry->insn_off = offset;
|
||||||
|
|
||||||
|
list_add_tail(&entry->list, orc_list);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int orc_create(struct objtool_file *file)
|
||||||
|
{
|
||||||
|
struct section *sec, *ip_rsec, *orc_sec;
|
||||||
|
unsigned int nr = 0, idx = 0;
|
||||||
|
struct orc_list_entry *entry;
|
||||||
|
struct list_head orc_list;
|
||||||
|
|
||||||
|
struct orc_entry null = {
|
||||||
|
.sp_reg = ORC_REG_UNDEFINED,
|
||||||
.bp_reg = ORC_REG_UNDEFINED,
|
.bp_reg = ORC_REG_UNDEFINED,
|
||||||
.type = UNWIND_HINT_TYPE_CALL,
|
.type = UNWIND_HINT_TYPE_CALL,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/* Build a deduplicated list of ORC entries: */
|
||||||
|
INIT_LIST_HEAD(&orc_list);
|
||||||
|
for_each_sec(file, sec) {
|
||||||
|
struct orc_entry orc, prev_orc = {0};
|
||||||
|
struct instruction *insn;
|
||||||
|
bool empty = true;
|
||||||
|
|
||||||
|
if (!sec->text)
|
||||||
|
continue;
|
||||||
|
|
||||||
|
sec_for_each_insn(file, sec, insn) {
|
||||||
|
if (init_orc_entry(&orc, &insn->cfi))
|
||||||
|
return -1;
|
||||||
|
if (!memcmp(&prev_orc, &orc, sizeof(orc)))
|
||||||
|
continue;
|
||||||
|
if (orc_list_add(&orc_list, &orc, sec, insn->offset))
|
||||||
|
return -1;
|
||||||
|
nr++;
|
||||||
|
prev_orc = orc;
|
||||||
|
empty = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Add a section terminator */
|
||||||
|
if (!empty) {
|
||||||
|
orc_list_add(&orc_list, &null, sec, sec->len);
|
||||||
|
nr++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!nr)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
/* Create .orc_unwind, .orc_unwind_ip and .rela.orc_unwind_ip sections: */
|
||||||
sec = find_section_by_name(file->elf, ".orc_unwind");
|
sec = find_section_by_name(file->elf, ".orc_unwind");
|
||||||
if (sec) {
|
if (sec) {
|
||||||
WARN("file already has .orc_unwind section, skipping");
|
WARN("file already has .orc_unwind section, skipping");
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
orc_sec = elf_create_section(file->elf, ".orc_unwind", 0,
|
||||||
/* count the number of needed orcs */
|
sizeof(struct orc_entry), nr);
|
||||||
idx = 0;
|
if (!orc_sec)
|
||||||
for_each_sec(file, sec) {
|
|
||||||
if (!sec->text)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
prev_insn = NULL;
|
|
||||||
sec_for_each_insn(file, sec, insn) {
|
|
||||||
if (!prev_insn ||
|
|
||||||
memcmp(&insn->orc, &prev_insn->orc,
|
|
||||||
sizeof(struct orc_entry))) {
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
prev_insn = insn;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* section terminator */
|
|
||||||
if (prev_insn)
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
if (!idx)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), nr);
|
||||||
/* create .orc_unwind_ip and .rela.orc_unwind_ip sections */
|
|
||||||
sec = elf_create_section(file->elf, ".orc_unwind_ip", 0, sizeof(int), idx);
|
|
||||||
if (!sec)
|
if (!sec)
|
||||||
return -1;
|
return -1;
|
||||||
|
ip_rsec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
|
||||||
ip_relocsec = elf_create_reloc_section(file->elf, sec, SHT_RELA);
|
if (!ip_rsec)
|
||||||
if (!ip_relocsec)
|
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
/* create .orc_unwind section */
|
/* Write ORC entries to sections: */
|
||||||
u_sec = elf_create_section(file->elf, ".orc_unwind", 0,
|
list_for_each_entry(entry, &orc_list, list) {
|
||||||
sizeof(struct orc_entry), idx);
|
if (write_orc_entry(file->elf, orc_sec, ip_rsec, idx++,
|
||||||
|
entry->insn_sec, entry->insn_off,
|
||||||
/* populate sections */
|
&entry->orc))
|
||||||
idx = 0;
|
return -1;
|
||||||
for_each_sec(file, sec) {
|
|
||||||
if (!sec->text)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
prev_insn = NULL;
|
|
||||||
sec_for_each_insn(file, sec, insn) {
|
|
||||||
if (!prev_insn || memcmp(&insn->orc, &prev_insn->orc,
|
|
||||||
sizeof(struct orc_entry))) {
|
|
||||||
|
|
||||||
if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx,
|
|
||||||
insn->sec, insn->offset,
|
|
||||||
&insn->orc))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
prev_insn = insn;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* section terminator */
|
|
||||||
if (prev_insn) {
|
|
||||||
if (create_orc_entry(file->elf, u_sec, ip_relocsec, idx,
|
|
||||||
prev_insn->sec,
|
|
||||||
prev_insn->offset + prev_insn->len,
|
|
||||||
&empty))
|
|
||||||
return -1;
|
|
||||||
|
|
||||||
idx++;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (elf_rebuild_reloc_section(file->elf, ip_relocsec))
|
if (elf_rebuild_reloc_section(file->elf, ip_rsec))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -25,12 +25,7 @@ int __weak orc_dump(const char *_objname)
|
||||||
UNSUPPORTED("orc");
|
UNSUPPORTED("orc");
|
||||||
}
|
}
|
||||||
|
|
||||||
int __weak create_orc(struct objtool_file *file)
|
int __weak orc_create(struct objtool_file *file)
|
||||||
{
|
|
||||||
UNSUPPORTED("orc");
|
|
||||||
}
|
|
||||||
|
|
||||||
int __weak create_orc_sections(struct objtool_file *file)
|
|
||||||
{
|
{
|
||||||
UNSUPPORTED("orc");
|
UNSUPPORTED("orc");
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue