module.c 3.6 KB

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