linux/arch/unicore32/kernel/module.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * linux/arch/unicore32/kernel/module.c
   4 *
   5 * Code specific to PKUnity SoC and UniCore ISA
   6 *
   7 * Copyright (C) 2001-2010 GUAN Xue-tao
   8 */
   9#include <linux/module.h>
  10#include <linux/moduleloader.h>
  11#include <linux/kernel.h>
  12#include <linux/mm.h>
  13#include <linux/elf.h>
  14#include <linux/vmalloc.h>
  15#include <linux/fs.h>
  16#include <linux/string.h>
  17#include <linux/gfp.h>
  18
  19#include <asm/sections.h>
  20
  21void *module_alloc(unsigned long size)
  22{
  23        return __vmalloc_node_range(size, 1, MODULES_VADDR, MODULES_END,
  24                                GFP_KERNEL, PAGE_KERNEL_EXEC, 0, NUMA_NO_NODE,
  25                                __builtin_return_address(0));
  26}
  27
  28int
  29apply_relocate(Elf32_Shdr *sechdrs, const char *strtab, unsigned int symindex,
  30               unsigned int relindex, struct module *module)
  31{
  32        Elf32_Shdr *symsec = sechdrs + symindex;
  33        Elf32_Shdr *relsec = sechdrs + relindex;
  34        Elf32_Shdr *dstsec = sechdrs + relsec->sh_info;
  35        Elf32_Rel *rel = (void *)relsec->sh_addr;
  36        unsigned int i;
  37
  38        for (i = 0; i < relsec->sh_size / sizeof(Elf32_Rel); i++, rel++) {
  39                unsigned long loc;
  40                Elf32_Sym *sym;
  41                s32 offset;
  42
  43                offset = ELF32_R_SYM(rel->r_info);
  44                if (offset < 0 || offset >
  45                                (symsec->sh_size / sizeof(Elf32_Sym))) {
  46                        printk(KERN_ERR "%s: bad relocation, "
  47                                        "section %d reloc %d\n",
  48                                        module->name, relindex, i);
  49                        return -ENOEXEC;
  50                }
  51
  52                sym = ((Elf32_Sym *)symsec->sh_addr) + offset;
  53
  54                if (rel->r_offset < 0 || rel->r_offset >
  55                                dstsec->sh_size - sizeof(u32)) {
  56                        printk(KERN_ERR "%s: out of bounds relocation, "
  57                                "section %d reloc %d offset %d size %d\n",
  58                                module->name, relindex, i, rel->r_offset,
  59                                dstsec->sh_size);
  60                        return -ENOEXEC;
  61                }
  62
  63                loc = dstsec->sh_addr + rel->r_offset;
  64
  65                switch (ELF32_R_TYPE(rel->r_info)) {
  66                case R_UNICORE_NONE:
  67                        /* ignore */
  68                        break;
  69
  70                case R_UNICORE_ABS32:
  71                        *(u32 *)loc += sym->st_value;
  72                        break;
  73
  74                case R_UNICORE_PC24:
  75                case R_UNICORE_CALL:
  76                case R_UNICORE_JUMP24:
  77                        offset = (*(u32 *)loc & 0x00ffffff) << 2;
  78                        if (offset & 0x02000000)
  79                                offset -= 0x04000000;
  80
  81                        offset += sym->st_value - loc;
  82                        if (offset & 3 ||
  83                            offset <= (s32)0xfe000000 ||
  84                            offset >= (s32)0x02000000) {
  85                                printk(KERN_ERR
  86                                       "%s: relocation out of range, section "
  87                                       "%d reloc %d sym '%s'\n", module->name,
  88                                       relindex, i, strtab + sym->st_name);
  89                                return -ENOEXEC;
  90                        }
  91
  92                        offset >>= 2;
  93
  94                        *(u32 *)loc &= 0xff000000;
  95                        *(u32 *)loc |= offset & 0x00ffffff;
  96                        break;
  97
  98                default:
  99                        printk(KERN_ERR "%s: unknown relocation: %u\n",
 100                               module->name, ELF32_R_TYPE(rel->r_info));
 101                        return -ENOEXEC;
 102                }
 103        }
 104        return 0;
 105}
 106