do-rel.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #define elf_dynamic_redo_rel elf_dynamic_redo_rela
  29. #ifndef DO_ELF_MACHINE_REL_RELATIVE
  30. # define DO_ELF_MACHINE_REL_RELATIVE(l, relative) \
  31. elf_machine_rel_relative (l, relative, \
  32. (void *) (l->l_addr + relative->r_offset))
  33. #endif
  34. #ifndef VERSYMIDX
  35. # define VERSYMIDX(sym) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGIDX (sym))
  36. #endif
  37. #ifndef VALIDX
  38. # define VALIDX(tag) (DT_NUM + DT_THISPROCNUM + DT_VERSIONTAGNUM \
  39. + DT_EXTRANUM + DT_VALTAGIDX (tag))
  40. #endif
  41. #define elf_dynamic_copy_rel elf_dynamic_copy_rela
  42. #define dt_reloc DT_RELA
  43. #define dt_reloc_sz DT_RELASZ
  44. /* Perform the relocations in MAP on the running program image as specified
  45. by RELTAG, SZTAG. If LAZY is nonzero, this is the first pass on PLT
  46. relocations; they should be set up to call _dl_runtime_resolve, rather
  47. than fully resolved now. */
  48. static void __attribute__((unused))
  49. elf_dynamic_do_rel (struct link_map * l, ElfW(Addr) reladdr, int relsize)
  50. {
  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