do-rel.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * do-rel.c
  17. *
  18. * This file contains architecture-independent codes for relocating ELF
  19. * binaries.
  20. * Most of the source codes are imported from GNU C library.
  21. */
  22. #include "dl-machine-x86_64.h"
  23. #define elf_dynamic_do_rel elf_dynamic_do_rela
  24. #define RELCOUNT_IDX VERSYMIDX (DT_RELACOUNT)
  25. #define Rel Rela
  26. #define elf_machine_rel elf_machine_rela
  27. #define elf_machine_rel_relative elf_machine_rela_relative
  28. #ifndef DO_ELF_MACHINE_REL_RELATIVE
  29. # define DO_ELF_MACHINE_REL_RELATIVE(l, relative) \
  30. elf_machine_rel_relative (l, relative, \
  31. (void *) (l->l_addr + relative->r_offset))
  32. #endif
  33. #ifndef VERSYMIDX
  34. # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
  35. #endif
  36. #ifndef VALIDX
  37. # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
  38. + DT_EXTRANUM + DT_VALTAGIDX (tag))
  39. #endif
  40. #define elf_dynamic_copy_rel elf_dynamic_copy_rela
  41. #define dt_reloc DT_RELA
  42. #define dt_reloc_sz DT_RELASZ
  43. /* Perform the relocations in MAP on the running program image as specified
  44. by RELTAG, SZTAG. If LAZY is nonzero, this is the first pass on PLT
  45. relocations; they should be set up to call _dl_runtime_resolve, rather
  46. than fully resolved now. */
  47. static void __attribute__((unused))
  48. elf_dynamic_do_rel (struct link_map * l, ElfW(Addr) reladdr, int relsize)
  49. {
  50. ElfW(Sym) * symtab = (void *) D_PTR (l->l_info[DT_SYMTAB]);
  51. ElfW(Rel) * r = (void *) reladdr;
  52. ElfW(Rel) * end = (void *) (reladdr + relsize);
  53. ElfW(Word) nrelative = l->l_info[RELCOUNT_IDX] == NULL
  54. ? 0 : l->l_info[RELCOUNT_IDX]->d_un.d_val;
  55. /* ElfW(Rel) * relative = r; */
  56. int nrelsize = relsize / sizeof (ElfW(Rel));
  57. r = r + (nrelative < nrelsize ? nrelative : nrelsize);
  58. for (; r < end; ++r)
  59. elf_machine_rel(l, r, &symtab[ELFW(R_SYM) (r->r_info)],
  60. (void *) (l->l_addr + r->r_offset));
  61. }
  62. static void inline elf_copy_rel (struct link_map * l1, struct link_map * l2,
  63. int reloc, int reloc_sz)
  64. {
  65. if (!l1->l_info[reloc] || !l2->l_info[reloc])
  66. return;
  67. ElfW(Sym) * symtab1 = (void *) D_PTR (l1->l_info[DT_SYMTAB]);
  68. const char * strtab1 = (void *) D_PTR (l1->l_info[DT_STRTAB]);
  69. ElfW(Sym) * symtab2 = (void *) D_PTR (l2->l_info[DT_SYMTAB]);
  70. const char * strtab2 = (void *) D_PTR (l2->l_info[DT_STRTAB]);
  71. ElfW(Rel) * r1, * r2, * end1, * end2;
  72. r1 = (ElfW(Rel) *) D_PTR (l1->l_info[reloc]);
  73. end1 = ((void *) r1 + l1->l_info[reloc_sz]->d_un.d_val);
  74. r1 += l1->l_info[RELCOUNT_IDX] ? l1->l_info[RELCOUNT_IDX]->d_un.d_val : 0;
  75. r2 = (ElfW(Rel) *) D_PTR (l2->l_info[reloc]);
  76. end2 = ((void *) r2 + l2->l_info[reloc_sz]->d_un.d_val);
  77. r2 += l2->l_info[RELCOUNT_IDX] ? l2->l_info[RELCOUNT_IDX]->d_un.d_val : 0;
  78. for (; r1 < end1 && r2 < end2; ++r1, ++r2) {
  79. debug("copy %s from %s\n",
  80. strtab1 + symtab1[ELFW(R_SYM) (r1->r_info)].st_name,
  81. strtab2 + symtab2[ELFW(R_SYM) (r2->r_info)].st_name);
  82. r1->r_info = r2->r_info;
  83. ElfW(Addr) * reladdr1 = (void *) l1->l_addr + r1->r_offset;
  84. ElfW(Addr) * reladdr2 = (void *) l2->l_addr + r2->r_offset;
  85. if (*reladdr1 != *reladdr2)
  86. *reladdr1 = *reladdr2;
  87. }
  88. }
  89. /* copy the relocation done by PAL */
  90. static void __attribute__((unused))
  91. elf_dynamic_copy_rel (struct link_map * l1, struct link_map * l2)
  92. {
  93. elf_copy_rel(l1, l2, dt_reloc, dt_reloc_sz);
  94. elf_copy_rel(l1, l2, DT_JMPREL, DT_PLTRELSZ);
  95. }
  96. #undef elf_dynamic_do_rel
  97. #undef Rel
  98. #undef elf_machine_rel
  99. #undef elf_machine_rel_relative
  100. #undef DO_ELF_MACHINE_REL_RELATIVE
  101. #undef RELCOUNT_IDX
  102. #undef elf_dynamic_copy_rel
  103. #undef dt_reloc
  104. #undef dt_reloc_sz