module.c 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /* Kernel module help for i386.
  2. Copyright (C) 2001 Rusty Russell.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  14. */
  15. #include <linux/moduleloader.h>
  16. #include <linux/elf.h>
  17. #include <linux/vmalloc.h>
  18. #include <linux/fs.h>
  19. #include <linux/string.h>
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #if 0
  23. #define DEBUGP printk
  24. #else
  25. #define DEBUGP(fmt , ...)
  26. #endif
  27. #ifdef CONFIG_ETRAX_KMALLOCED_MODULES
  28. void *module_alloc(unsigned long size)
  29. {
  30. return kmalloc(size, GFP_KERNEL);
  31. }
  32. /* Free memory returned from module_alloc */
  33. void module_memfree(void *module_region)
  34. {
  35. kfree(module_region);
  36. }
  37. #endif
  38. int apply_relocate_add(Elf32_Shdr *sechdrs,
  39. const char *strtab,
  40. unsigned int symindex,
  41. unsigned int relsec,
  42. struct module *me)
  43. {
  44. unsigned int i;
  45. Elf32_Rela *rela = (void *)sechdrs[relsec].sh_addr;
  46. DEBUGP ("Applying add relocate section %u to %u\n", relsec,
  47. sechdrs[relsec].sh_info);
  48. for (i = 0; i < sechdrs[relsec].sh_size / sizeof (*rela); i++) {
  49. /* This is where to make the change */
  50. uint32_t *loc
  51. = ((void *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  52. + rela[i].r_offset);
  53. /* This is the symbol it is referring to. Note that all
  54. undefined symbols have been resolved. */
  55. Elf32_Sym *sym
  56. = ((Elf32_Sym *)sechdrs[symindex].sh_addr
  57. + ELF32_R_SYM (rela[i].r_info));
  58. switch (ELF32_R_TYPE(rela[i].r_info)) {
  59. case R_CRIS_32:
  60. *loc = sym->st_value + rela[i].r_addend;
  61. break;
  62. case R_CRIS_32_PCREL:
  63. *loc = sym->st_value - (unsigned)loc + rela[i].r_addend - 4;
  64. break;
  65. default:
  66. printk(KERN_ERR "module %s: Unknown relocation: %u\n",
  67. me->name, ELF32_R_TYPE(rela[i].r_info));
  68. return -ENOEXEC;
  69. }
  70. }
  71. return 0;
  72. }