dl.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. /* dl-386.c - arch-dependent part of loadable module support */
  2. /*
  3. * GRUB -- GRand Unified Bootloader
  4. * Copyright (C) 2002,2005,2007,2009 Free Software Foundation, Inc.
  5. *
  6. * GRUB is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * GRUB is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <grub/dl.h>
  20. #include <grub/elf.h>
  21. #include <grub/misc.h>
  22. #include <grub/err.h>
  23. #include <grub/i18n.h>
  24. /* Check if EHDR is a valid ELF header. */
  25. grub_err_t
  26. grub_arch_dl_check_header (void *ehdr)
  27. {
  28. Elf_Ehdr *e = ehdr;
  29. /* Check the magic numbers. */
  30. if (e->e_ident[EI_CLASS] != ELFCLASS32
  31. || e->e_ident[EI_DATA] != ELFDATA2LSB
  32. || e->e_machine != EM_386)
  33. return grub_error (GRUB_ERR_BAD_OS, N_("invalid arch-dependent ELF magic"));
  34. return GRUB_ERR_NONE;
  35. }
  36. /* Relocate symbols. */
  37. grub_err_t
  38. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr,
  39. Elf_Shdr *s, grub_dl_segment_t seg)
  40. {
  41. Elf_Rel *rel, *max;
  42. for (rel = (Elf_Rel *) ((char *) ehdr + s->sh_offset),
  43. max = (Elf_Rel *) ((char *) rel + s->sh_size);
  44. rel < max;
  45. rel = (Elf_Rel *) ((char *) rel + s->sh_entsize))
  46. {
  47. Elf_Word *addr;
  48. Elf_Sym *sym;
  49. if (seg->size < rel->r_offset)
  50. return grub_error (GRUB_ERR_BAD_MODULE,
  51. "reloc offset is out of the segment");
  52. addr = (Elf_Word *) ((char *) seg->addr + rel->r_offset);
  53. sym = (Elf_Sym *) ((char *) mod->symtab
  54. + mod->symsize * ELF_R_SYM (rel->r_info));
  55. switch (ELF_R_TYPE (rel->r_info))
  56. {
  57. case R_386_32:
  58. *addr += sym->st_value;
  59. break;
  60. case R_386_PC32:
  61. *addr += (sym->st_value - (grub_addr_t) addr);
  62. break;
  63. default:
  64. return grub_error (GRUB_ERR_NOT_IMPLEMENTED_YET,
  65. N_("relocation 0x%x is not implemented yet"),
  66. ELF_R_TYPE (rel->r_info));
  67. }
  68. }
  69. return GRUB_ERR_NONE;
  70. }