module.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /* Kernel module help for sparc32.
  2. *
  3. * Copyright (C) 2001 Rusty Russell.
  4. * Copyright (C) 2002 David S. Miller.
  5. */
  6. #include <linux/moduleloader.h>
  7. #include <linux/kernel.h>
  8. #include <linux/elf.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/fs.h>
  11. #include <linux/string.h>
  12. void *module_alloc(unsigned long size)
  13. {
  14. void *ret;
  15. /* We handle the zero case fine, unlike vmalloc */
  16. if (size == 0)
  17. return NULL;
  18. ret = vmalloc(size);
  19. if (!ret)
  20. ret = ERR_PTR(-ENOMEM);
  21. else
  22. memset(ret, 0, size);
  23. return ret;
  24. }
  25. /* Free memory returned from module_core_alloc/module_init_alloc */
  26. void module_free(struct module *mod, void *module_region)
  27. {
  28. vfree(module_region);
  29. /* FIXME: If module_region == mod->init_region, trim exception
  30. table entries. */
  31. }
  32. /* Make generic code ignore STT_REGISTER dummy undefined symbols,
  33. * and replace references to .func with func as in ppc64's dedotify.
  34. */
  35. int module_frob_arch_sections(Elf_Ehdr *hdr,
  36. Elf_Shdr *sechdrs,
  37. char *secstrings,
  38. struct module *mod)
  39. {
  40. unsigned int symidx;
  41. Elf32_Sym *sym;
  42. char *strtab;
  43. int i;
  44. for (symidx = 0; sechdrs[symidx].sh_type != SHT_SYMTAB; symidx++) {
  45. if (symidx == hdr->e_shnum-1) {
  46. printk("%s: no symtab found.\n", mod->name);
  47. return -ENOEXEC;
  48. }
  49. }
  50. sym = (Elf32_Sym *)sechdrs[symidx].sh_addr;
  51. strtab = (char *)sechdrs[sechdrs[symidx].sh_link].sh_addr;
  52. for (i = 1; i < sechdrs[symidx].sh_size / sizeof(Elf_Sym); i++) {
  53. if (sym[i].st_shndx == SHN_UNDEF) {
  54. if (ELF32_ST_TYPE(sym[i].st_info) == STT_REGISTER)
  55. sym[i].st_shndx = SHN_ABS;
  56. else {
  57. char *name = strtab + sym[i].st_name;
  58. if (name[0] == '.')
  59. memmove(name, name+1, strlen(name));
  60. }
  61. }
  62. }
  63. return 0;
  64. }
  65. int apply_relocate(Elf32_Shdr *sechdrs,
  66. const char *strtab,
  67. unsigned int symindex,
  68. unsigned int relsec,
  69. struct module *me)
  70. {
  71. printk(KERN_ERR "module %s: non-ADD RELOCATION unsupported\n",
  72. me->name);
  73. return -ENOEXEC;
  74. }
  75. int apply_relocate_add(Elf32_Shdr *sechdrs,
  76. const char *strtab,
  77. unsigned int symindex,
  78. unsigned int relsec,
  79. struct module *me)
  80. {
  81. unsigned int i;
  82. Elf32_Rela *rel = (void *)sechdrs[relsec].sh_addr;
  83. Elf32_Sym *sym;
  84. u8 *location;
  85. u32 *loc32;
  86. for (i = 0; i < sechdrs[relsec].sh_size / sizeof(*rel); i++) {
  87. Elf32_Addr v;
  88. /* This is where to make the change */
  89. location = (u8 *)sechdrs[sechdrs[relsec].sh_info].sh_addr
  90. + rel[i].r_offset;
  91. loc32 = (u32 *) location;
  92. /* This is the symbol it is referring to. Note that all
  93. undefined symbols have been resolved. */
  94. sym = (Elf32_Sym *)sechdrs[symindex].sh_addr
  95. + ELF32_R_SYM(rel[i].r_info);
  96. v = sym->st_value + rel[i].r_addend;
  97. switch (ELF32_R_TYPE(rel[i].r_info)) {
  98. case R_SPARC_32:
  99. location[0] = v >> 24;
  100. location[1] = v >> 16;
  101. location[2] = v >> 8;
  102. location[3] = v >> 0;
  103. break;
  104. case R_SPARC_WDISP30:
  105. v -= (Elf32_Addr) location;
  106. *loc32 = (*loc32 & ~0x3fffffff) |
  107. ((v >> 2) & 0x3fffffff);
  108. break;
  109. case R_SPARC_WDISP22:
  110. v -= (Elf32_Addr) location;
  111. *loc32 = (*loc32 & ~0x3fffff) |
  112. ((v >> 2) & 0x3fffff);
  113. break;
  114. case R_SPARC_LO10:
  115. *loc32 = (*loc32 & ~0x3ff) | (v & 0x3ff);
  116. break;
  117. case R_SPARC_HI22:
  118. *loc32 = (*loc32 & ~0x3fffff) |
  119. ((v >> 10) & 0x3fffff);
  120. break;
  121. default:
  122. printk(KERN_ERR "module %s: Unknown relocation: %x\n",
  123. me->name,
  124. (int) (ELF32_R_TYPE(rel[i].r_info) & 0xff));
  125. return -ENOEXEC;
  126. };
  127. }
  128. return 0;
  129. }
  130. int module_finalize(const Elf_Ehdr *hdr,
  131. const Elf_Shdr *sechdrs,
  132. struct module *me)
  133. {
  134. return 0;
  135. }
  136. void module_arch_cleanup(struct module *mod)
  137. {
  138. }