do-rel.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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, \
  30. (void*)((l)->l_addr + relative->r_offset))
  31. #endif
  32. #ifndef VERSYMIDX
  33. # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX(sym))
  34. #endif
  35. #ifndef VALIDX
  36. # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
  37. + DT_EXTRANUM + DT_VALTAGIDX(tag))
  38. #endif
  39. #define elf_dynamic_copy_rel elf_dynamic_copy_rela
  40. #define dt_reloc DT_RELA
  41. #define dt_reloc_sz DT_RELASZ
  42. /* Perform the relocations in MAP on the running program image as specified
  43. by RELTAG, SZTAG. If LAZY is nonzero, this is the first pass on PLT
  44. relocations; they should be set up to call _dl_runtime_resolve, rather
  45. than fully resolved now. */
  46. static void __attribute__((unused))
  47. elf_dynamic_do_rel (struct link_map * l, ElfW(Addr) reladdr, int relsize)
  48. {
  49. if (!l->l_info[DT_SYMTAB])
  50. return;
  51. ElfW(Sym) * symtab = (void *) D_PTR (l->l_info[DT_SYMTAB]);
  52. ElfW(Rel) * r = (void *) reladdr;
  53. ElfW(Rel) * end = (void *) (reladdr + relsize);
  54. ElfW(Word) nrelative = l->l_info[RELCOUNT_IDX] == NULL
  55. ? 0 : l->l_info[RELCOUNT_IDX]->d_un.d_val;
  56. int nrelsize = relsize / sizeof (ElfW(Rel));
  57. r = r + (nrelative < nrelsize ? nrelative : nrelsize);
  58. for (; r < end; ++r) {
  59. ElfW(Sym) * sym = &symtab[ELFW(R_SYM) (r->r_info)];
  60. void * reloc = (void *) l->l_addr + r->r_offset;
  61. if (elf_machine_rel(l, r, sym, reloc)) {
  62. assert(l->nlinksyms < MAX_LINKSYMS);
  63. l->linksyms[l->nlinksyms].rel = r;
  64. l->linksyms[l->nlinksyms].sym = sym;
  65. l->linksyms[l->nlinksyms].reloc = reloc;
  66. l->nlinksyms++;
  67. }
  68. }
  69. }
  70. static void __attribute__((unused))
  71. elf_dynamic_redo_rel (struct link_map * l)
  72. {
  73. for (int i = 0 ; i < l->nlinksyms ; i++)
  74. elf_machine_rel(l, l->linksyms[i].rel,
  75. l->linksyms[i].sym,
  76. l->linksyms[i].reloc);
  77. }
  78. #if 0
  79. static void inline elf_copy_rel (struct link_map * l1, struct link_map * l2,
  80. int reloc, int reloc_sz)
  81. {
  82. if (!l1->l_info[reloc] || !l2->l_info[reloc])
  83. return;
  84. ElfW(Sym) * symtab1 = (void *) D_PTR (l1->l_info[DT_SYMTAB]);
  85. const char * strtab1 = (void *) D_PTR (l1->l_info[DT_STRTAB]);
  86. ElfW(Sym) * symtab2 = (void *) D_PTR (l2->l_info[DT_SYMTAB]);
  87. const char * strtab2 = (void *) D_PTR (l2->l_info[DT_STRTAB]);
  88. ElfW(Rel) * r1, * r2, * end1, * end2;
  89. r1 = (ElfW(Rel) *) D_PTR (l1->l_info[reloc]);
  90. end1 = ((void *) r1 + l1->l_info[reloc_sz]->d_un.d_val);
  91. r1 += l1->l_info[RELCOUNT_IDX] ? l1->l_info[RELCOUNT_IDX]->d_un.d_val : 0;
  92. r2 = (ElfW(Rel) *) D_PTR (l2->l_info[reloc]);
  93. end2 = ((void *) r2 + l2->l_info[reloc_sz]->d_un.d_val);
  94. r2 += l2->l_info[RELCOUNT_IDX] ? l2->l_info[RELCOUNT_IDX]->d_un.d_val : 0;
  95. for (; r1 < end1 && r2 < end2; ++r1, ++r2) {
  96. debug("copy %s from %s\n",
  97. strtab1 + symtab1[ELFW(R_SYM) (r1->r_info)].st_name,
  98. strtab2 + symtab2[ELFW(R_SYM) (r2->r_info)].st_name);
  99. r1->r_info = r2->r_info;
  100. ElfW(Addr) * reladdr1 = (void *) l1->l_addr + r1->r_offset;
  101. ElfW(Addr) * reladdr2 = (void *) l2->l_addr + r2->r_offset;
  102. if (*reladdr1 != *reladdr2)
  103. *reladdr1 = *reladdr2;
  104. }
  105. }
  106. /* copy the relocation done by PAL */
  107. static void __attribute__((unused))
  108. elf_dynamic_copy_rel (struct link_map * l1, struct link_map * l2)
  109. {
  110. elf_copy_rel(l1, l2, dt_reloc, dt_reloc_sz);
  111. elf_copy_rel(l1, l2, DT_JMPREL, DT_PLTRELSZ);
  112. }
  113. #endif
  114. #undef elf_dynamic_do_rel
  115. #undef Rel
  116. #undef elf_machine_rel
  117. #undef elf_machine_rel_relative
  118. #undef DO_ELF_MACHINE_REL_RELATIVE
  119. #undef RELCOUNT_IDX
  120. //#undef elf_dynamic_copy_rel
  121. #undef dt_reloc
  122. #undef dt_reloc_sz
  123. #undef elf_dynamic_redo_rel