struct special_entry describes a special section entry with a length of
the original and of the replacement code, in bytes. Powerpc tables store
where the code ends instead, as a relocation against the end label.

For these entries, we compute orig_len and new_len, with help of
find_reloc_by_dest and reloc_to_sec_off function. Parse these entries
when --alt-fixup is provided.

BEGIN_FTR_SECTION/END_FTR_SECTION and friends record, for each pair of
original and replacement code, an entry in __ftr_fixup, __mmu_ftr_fixup
or __fw_ftr_fixup (see MAKE_FTR_SECTION_ENTRY()):

        word mask, value, start, end, alt_start, alt_end;

Add these sections to special_entries, so they can be parsed.

Signed-off-by: Shivang Upadhyay <[email protected]>
---
 .../arch/powerpc/include/arch/special.h       | 16 ++++
 tools/objtool/special.c                       | 80 ++++++++++++++++++-
 2 files changed, 93 insertions(+), 3 deletions(-)

diff --git a/tools/objtool/arch/powerpc/include/arch/special.h 
b/tools/objtool/arch/powerpc/include/arch/special.h
index ffef9ada7133..5667a476fd16 100644
--- a/tools/objtool/arch/powerpc/include/arch/special.h
+++ b/tools/objtool/arch/powerpc/include/arch/special.h
@@ -18,4 +18,20 @@
 #define ALT_ORIG_LEN_OFFSET 10
 #define ALT_NEW_LEN_OFFSET 11
 
+/*
+ * Feature fixup entries (__ftr_fixup, __mmu_ftr_fixup, __fw_ftr_fixup), see
+ * MAKE_FTR_SECTION_ENTRY() in asm/feature-fixups.h:
+ *
+ *     word mask, value;
+ *     word start, end, alt_start, alt_end;
+ *
+ * The sizes below are in words, objtool multiplies them by the ELF word size.
+ */
+
+#define FTR_ENTRY_SIZE         6
+#define FTR_ORIG_OFFSET                2
+#define FTR_ORIG_END_OFFSET    3
+#define FTR_NEW_OFFSET         4
+#define FTR_NEW_END_OFFSET     5
+
 #endif /* _PPC_ARCH_SPECIAL_H */
diff --git a/tools/objtool/special.c b/tools/objtool/special.c
index 2a533afbc69a..31420134d77e 100644
--- a/tools/objtool/special.c
+++ b/tools/objtool/special.c
@@ -23,9 +23,26 @@ struct special_entry {
        unsigned char orig_len, new_len; /* group only */
        unsigned char feature; /* ALTERNATIVE macro CPU feature */
        unsigned char key; /* jump_label key */
+
+       // --alt-fixup entries, read by nothing else.
+       bool fixup;
+       int orig_end, new_end;
 };
 
-static const struct special_entry entries[] = {
+/* powerpc feature fixups, see arch/powerpc/include/asm/feature-fixups.h */
+#define FTR_FIXUP_ENTRY(name)                  \
+       {                                       \
+               .sec = name,                    \
+               .group = true,                  \
+               .size = FTR_ENTRY_SIZE,         \
+               .orig = FTR_ORIG_OFFSET,        \
+               .new = FTR_NEW_OFFSET,          \
+               .fixup = true,                  \
+               .orig_end = FTR_ORIG_END_OFFSET,\
+               .new_end = FTR_NEW_END_OFFSET,  \
+       }
+
+static struct special_entry entries[] = {
        {
                .sec = ".altinstructions",
                .group = true,
@@ -50,6 +67,11 @@ static const struct special_entry entries[] = {
                .orig = EX_ORIG_OFFSET,
                .new = EX_NEW_OFFSET,
        },
+#ifdef FTR_ENTRY_SIZE
+       FTR_FIXUP_ENTRY("__ftr_fixup"),
+       FTR_FIXUP_ENTRY("__mmu_ftr_fixup"),
+       FTR_FIXUP_ENTRY("__fw_ftr_fixup"),
+#endif
        {},
 };
 
@@ -93,9 +115,25 @@ static int get_alt_entry(struct elf *elf, const struct 
special_entry *entry,
 
        reloc_to_sec_off(orig_reloc, &alt->orig_sec, &alt->orig_off);
 
+       if (entry->fixup) {
+                       struct section *end_sec;
+                       unsigned long end_off;
+
+                       alt->orig_off -= entry->orig;
+                       struct reloc *end_reloc = find_reloc_by_dest(elf, sec, 
idx * entry->size + entry->orig_end);
+                       if (!end_reloc) {
+                               ERROR_FUNC(sec,  idx * entry->size + 
entry->orig_end, "can't find reloc");
+                               return -1;
+                       }
+                       reloc_to_sec_off(end_reloc, &end_sec, &end_off);
+                       end_off -= entry->orig_end;
+
+                       alt->orig_len = end_off - alt->orig_off;
+       }
+
        arch_handle_alternative(alt);
 
-       if (!entry->group || alt->new_len) {
+       if (!entry->group || alt->new_len || entry->fixup) {
                new_reloc = find_reloc_by_dest(elf, sec, offset + entry->new);
                if (!new_reloc) {
                        ERROR_FUNC(sec, offset + entry->new, "can't find new 
reloc");
@@ -104,6 +142,22 @@ static int get_alt_entry(struct elf *elf, const struct 
special_entry *entry,
 
                reloc_to_sec_off(new_reloc, &alt->new_sec, &alt->new_off);
 
+               if (entry->fixup) {
+                       struct section *end_sec;
+                       unsigned long end_off;
+
+                       alt->new_off -= entry->new;
+                       struct reloc *end_reloc = find_reloc_by_dest(elf, sec, 
idx * entry->size + entry->new_end);
+                       if (!end_reloc) {
+                               ERROR_FUNC(sec,  idx * entry->size + 
entry->new_end, "can't find reloc");
+                               return -1;
+                       }
+                       reloc_to_sec_off(end_reloc, &end_sec, &end_off);
+                       end_off -= entry->new_end;
+
+                       alt->new_len = end_off - alt->new_off;
+               }
+
                /* _ASM_EXTABLE_EX hack */
                if (alt->new_off >= 0x7ffffff0)
                        alt->new_off -= 0x7ffffff0;
@@ -130,7 +184,7 @@ static int get_alt_entry(struct elf *elf, const struct 
special_entry *entry,
  */
 int special_get_alts(struct elf *elf, struct list_head *alts)
 {
-       const struct special_entry *entry;
+       struct special_entry *entry;
        struct section *sec;
        unsigned int nr_entries;
        struct special_alt *alt;
@@ -139,6 +193,17 @@ int special_get_alts(struct elf *elf, struct list_head 
*alts)
        INIT_LIST_HEAD(alts);
 
        for (entry = entries; entry->sec; entry++) {
+               /* The fixup layout is given in words, scale it to the ELF word 
size */
+               if (entry->fixup) {
+                       size_t word = elf_addr_size(elf);
+
+                       entry->size *= word;
+                       entry->orig *= word;
+                       entry->new *= word;
+                       entry->orig_end *= word;
+                       entry->new_end *= word;
+               }
+
                sec = find_section_by_name(elf, entry->sec);
                if (!sec)
                        continue;
@@ -161,6 +226,15 @@ int special_get_alts(struct elf *elf, struct list_head 
*alts)
                        if (get_alt_entry(elf, entry, sec, idx, alt))
                                return -1;
 
+                       /*
+                        * Nothing to copy and fix up if the original is just
+                        * nopped. Such an entry can overlap other ones.
+                        */
+                       if (entry->fixup && !alt->new_len) {
+                               free(alt);
+                               continue;
+                       }
+
                        list_add_tail(&alt->list, alts);
                }
        }
-- 
2.56.1


Reply via email to