do-rel.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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. if (!l->l_info[DT_SYMTAB])
  52. return;
  53. ElfW(Sym) * symtab = (void *) D_PTR (l->l_info[DT_SYMTAB]);
  54. ElfW(Rel) * r = (void *) reladdr;
  55. ElfW(Rel) * end = (void *) (reladdr + relsize);
  56. ElfW(Word) nrelative = l->l_info[RELCOUNT_IDX] == NULL
  57. ? 0 : l->l_info[RELCOUNT_IDX]->d_un.d_val;
  58. int nrelsize = relsize / sizeof (ElfW(Rel));
  59. r = r + (nrelative < nrelsize ? nrelative : nrelsize);
  60. for (; r < end; ++r) {
  61. ElfW(Sym) * sym = &symtab[ELFW(R_SYM) (r->r_info)];
  62. void * reloc = (void *) l->l_addr + r->r_offset;
  63. if (elf_machine_rel(l, r, sym, reloc)) {
  64. assert(l->nlinksyms < MAX_LINKSYMS);
  65. l->linksyms[l->nlinksyms].rel = r;
  66. l->linksyms[l->nlinksyms].sym = sym;
  67. l->linksyms[l->nlinksyms].reloc = reloc;
  68. l->nlinksyms++;
  69. }
  70. }
  71. }
  72. static void __attribute__((unused))
  73. elf_dynamic_redo_rel (struct link_map * l)
  74. {
  75. for (int i = 0 ; i < l->nlinksyms ; i++)
  76. elf_machine_rel(l, l->linksyms[i].rel,
  77. l->linksyms[i].sym,
  78. l->linksyms[i].reloc);
  79. }
  80. #if 0
  81. static void inline elf_copy_rel (struct link_map * l1, struct link_map * l2,
  82. int reloc, int reloc_sz)
  83. {
  84. if (!l1->l_info[reloc] || !l2->l_info[reloc])
  85. return;
  86. ElfW(Sym) * symtab1 = (void *) D_PTR (l1->l_info[DT_SYMTAB]);
  87. const char * strtab1 = (void *) D_PTR (l1->l_info[DT_STRTAB]);
  88. ElfW(Sym) * symtab2 = (void *) D_PTR (l2->l_info[DT_SYMTAB]);
  89. const char * strtab2 = (void *) D_PTR (l2->l_info[DT_STRTAB]);
  90. ElfW(Rel) * r1, * r2, * end1, * end2;
  91. r1 = (ElfW(Rel) *) D_PTR (l1->l_info[reloc]);
  92. end1 = ((void *) r1 + l1->l_info[reloc_sz]->d_un.d_val);
  93. r1 += l1->l_info[RELCOUNT_IDX] ? l1->l_info[RELCOUNT_IDX]->d_un.d_val : 0;
  94. r2 = (ElfW(Rel) *) D_PTR (l2->l_info[reloc]);
  95. end2 = ((void *) r2 + l2->l_info[reloc_sz]->d_un.d_val);
  96. r2 += l2->l_info[RELCOUNT_IDX] ? l2->l_info[RELCOUNT_IDX]->d_un.d_val : 0;
  97. for (; r1 < end1 && r2 < end2; ++r1, ++r2) {
  98. debug("copy %s from %s\n",
  99. strtab1 + symtab1[ELFW(R_SYM) (r1->r_info)].st_name,
  100. strtab2 + symtab2[ELFW(R_SYM) (r2->r_info)].st_name);
  101. r1->r_info = r2->r_info;
  102. ElfW(Addr) * reladdr1 = (void *) l1->l_addr + r1->r_offset;
  103. ElfW(Addr) * reladdr2 = (void *) l2->l_addr + r2->r_offset;
  104. if (*reladdr1 != *reladdr2)
  105. *reladdr1 = *reladdr2;
  106. }
  107. }
  108. /* copy the relocation done by PAL */
  109. static void __attribute__((unused))
  110. elf_dynamic_copy_rel (struct link_map * l1, struct link_map * l2)
  111. {
  112. elf_copy_rel(l1, l2, dt_reloc, dt_reloc_sz);
  113. elf_copy_rel(l1, l2, DT_JMPREL, DT_PLTRELSZ);
  114. }
  115. #endif
  116. #undef elf_dynamic_do_rel
  117. #undef Rel
  118. #undef elf_machine_rel
  119. #undef elf_machine_rel_relative
  120. #undef DO_ELF_MACHINE_REL_RELATIVE
  121. #undef RELCOUNT_IDX
  122. //#undef elf_dynamic_copy_rel
  123. #undef dt_reloc
  124. #undef dt_reloc_sz
  125. #undef elf_dynamic_redo_rel