Merge tag 'block-5.14-2021-08-07' of git://git.kernel.dk/linux-block
[linux-2.6-microblaze.git] / arch / arm / kernel / module-plts.c
index 6e626ab..1fc309b 100644 (file)
@@ -4,6 +4,7 @@
  */
 
 #include <linux/elf.h>
+#include <linux/ftrace.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/sort.h>
 #include <asm/cache.h>
 #include <asm/opcodes.h>
 
-#define PLT_ENT_STRIDE         L1_CACHE_BYTES
-#define PLT_ENT_COUNT          (PLT_ENT_STRIDE / sizeof(u32))
-#define PLT_ENT_SIZE           (sizeof(struct plt_entries) / PLT_ENT_COUNT)
-
 #ifdef CONFIG_THUMB2_KERNEL
 #define PLT_ENT_LDR            __opcode_to_mem_thumb32(0xf8dff000 | \
                                                        (PLT_ENT_STRIDE - 4))
                                                    (PLT_ENT_STRIDE - 8))
 #endif
 
-struct plt_entries {
-       u32     ldr[PLT_ENT_COUNT];
-       u32     lit[PLT_ENT_COUNT];
+static const u32 fixed_plts[] = {
+#ifdef CONFIG_DYNAMIC_FTRACE
+       FTRACE_ADDR,
+       MCOUNT_ADDR,
+#endif
 };
 
 static bool in_init(const struct module *mod, unsigned long loc)
@@ -34,14 +33,40 @@ static bool in_init(const struct module *mod, unsigned long loc)
        return loc - (u32)mod->init_layout.base < mod->init_layout.size;
 }
 
+static void prealloc_fixed(struct mod_plt_sec *pltsec, struct plt_entries *plt)
+{
+       int i;
+
+       if (!ARRAY_SIZE(fixed_plts) || pltsec->plt_count)
+               return;
+       pltsec->plt_count = ARRAY_SIZE(fixed_plts);
+
+       for (i = 0; i < ARRAY_SIZE(plt->ldr); ++i)
+               plt->ldr[i] = PLT_ENT_LDR;
+
+       BUILD_BUG_ON(sizeof(fixed_plts) > sizeof(plt->lit));
+       memcpy(plt->lit, fixed_plts, sizeof(fixed_plts));
+}
+
 u32 get_module_plt(struct module *mod, unsigned long loc, Elf32_Addr val)
 {
        struct mod_plt_sec *pltsec = !in_init(mod, loc) ? &mod->arch.core :
                                                          &mod->arch.init;
+       struct plt_entries *plt;
+       int idx;
+
+       /* cache the address, ELF header is available only during module load */
+       if (!pltsec->plt_ent)
+               pltsec->plt_ent = (struct plt_entries *)pltsec->plt->sh_addr;
+       plt = pltsec->plt_ent;
 
-       struct plt_entries *plt = (struct plt_entries *)pltsec->plt->sh_addr;
-       int idx = 0;
+       prealloc_fixed(pltsec, plt);
+
+       for (idx = 0; idx < ARRAY_SIZE(fixed_plts); ++idx)
+               if (plt->lit[idx] == val)
+                       return (u32)&plt->ldr[idx];
 
+       idx = 0;
        /*
         * Look for an existing entry pointing to 'val'. Given that the
         * relocations are sorted, this will be the last entry we allocated.
@@ -189,8 +214,8 @@ static unsigned int count_plts(const Elf32_Sym *syms, Elf32_Addr base,
 int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
                              char *secstrings, struct module *mod)
 {
-       unsigned long core_plts = 0;
-       unsigned long init_plts = 0;
+       unsigned long core_plts = ARRAY_SIZE(fixed_plts);
+       unsigned long init_plts = ARRAY_SIZE(fixed_plts);
        Elf32_Shdr *s, *sechdrs_end = sechdrs + ehdr->e_shnum;
        Elf32_Sym *syms = NULL;
 
@@ -245,6 +270,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
        mod->arch.core.plt->sh_size = round_up(core_plts * PLT_ENT_SIZE,
                                               sizeof(struct plt_entries));
        mod->arch.core.plt_count = 0;
+       mod->arch.core.plt_ent = NULL;
 
        mod->arch.init.plt->sh_type = SHT_NOBITS;
        mod->arch.init.plt->sh_flags = SHF_EXECINSTR | SHF_ALLOC;
@@ -252,6 +278,7 @@ int module_frob_arch_sections(Elf_Ehdr *ehdr, Elf_Shdr *sechdrs,
        mod->arch.init.plt->sh_size = round_up(init_plts * PLT_ENT_SIZE,
                                               sizeof(struct plt_entries));
        mod->arch.init.plt_count = 0;
+       mod->arch.init.plt_ent = NULL;
 
        pr_debug("%s: plt=%x, init.plt=%x\n", __func__,
                 mod->arch.core.plt->sh_size, mod->arch.init.plt->sh_size);