do-rel.h 5.0 KB

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