dl.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. /* Check if EHDR is a valid ELF header. */
  24. grub_err_t
  25. grub_arch_dl_check_header (void *ehdr)
  26. {
  27. Elf_Ehdr *e = ehdr;
  28. /* Check the magic numbers. */
  29. if (e->e_ident[EI_CLASS] != ELFCLASS32
  30. || e->e_ident[EI_DATA] != ELFDATA2LSB
  31. || e->e_machine != EM_386)
  32. return grub_error (GRUB_ERR_BAD_OS, "invalid arch specific ELF magic");
  33. return GRUB_ERR_NONE;
  34. }
  35. /* Relocate symbols. */
  36. grub_err_t
  37. grub_arch_dl_relocate_symbols (grub_dl_t mod, void *ehdr)
  38. {
  39. Elf_Ehdr *e = ehdr;
  40. Elf_Shdr *s;
  41. Elf_Word entsize;
  42. unsigned i;
  43. /* Find a symbol table. */
  44. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  45. i < e->e_shnum;
  46. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  47. if (s->sh_type == SHT_SYMTAB)
  48. break;
  49. if (i == e->e_shnum)
  50. return grub_error (GRUB_ERR_BAD_MODULE, "no symtab found");
  51. entsize = s->sh_entsize;
  52. for (i = 0, s = (Elf_Shdr *) ((char *) e + e->e_shoff);
  53. i < e->e_shnum;
  54. i++, s = (Elf_Shdr *) ((char *) s + e->e_shentsize))
  55. if (s->sh_type == SHT_REL)
  56. {
  57. grub_dl_segment_t seg;
  58. /* Find the target segment. */
  59. for (seg = mod->segment; seg; seg = seg->next)
  60. if (seg->section == s->sh_info)
  61. break;
  62. if (seg)
  63. {
  64. Elf_Rel *rel, *max;
  65. for (rel = (Elf_Rel *) ((char *) e + s->sh_offset),
  66. max = rel + s->sh_size / s->sh_entsize;
  67. rel < max;
  68. rel++)
  69. {
  70. Elf_Word *addr;
  71. Elf_Sym *sym;
  72. if (seg->size < rel->r_offset)
  73. return grub_error (GRUB_ERR_BAD_MODULE,
  74. "reloc offset is out of the segment");
  75. addr = (Elf_Word *) ((char *) seg->addr + rel->r_offset);
  76. sym = (Elf_Sym *) ((char *) mod->symtab
  77. + entsize * ELF_R_SYM (rel->r_info));
  78. switch (ELF_R_TYPE (rel->r_info))
  79. {
  80. case R_386_32:
  81. *addr += sym->st_value;
  82. break;
  83. case R_386_PC32:
  84. *addr += (sym->st_value - (Elf_Word) seg->addr
  85. - rel->r_offset);
  86. break;
  87. }
  88. }
  89. }
  90. }
  91. return GRUB_ERR_NONE;
  92. }