do-rel.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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.h
  15. *
  16. * This files contain architecture-independent macros of ELF dynamic
  17. * relocation function.
  18. * The source code is imported and modified from the GNU C Library.
  19. */
  20. #include <pal_rtld.h>
  21. #include "dl-machine-x86_64.h"
  22. #define elf_dynamic_do_rel elf_dynamic_do_rela
  23. #define RELCOUNT_IDX VERSYMIDX(DT_RELACOUNT)
  24. #define Rel Rela
  25. #define elf_machine_rel elf_machine_rela
  26. #define elf_machine_rel_relative elf_machine_rela_relative
  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. static void __attribute_unused elf_dynamic_do_rel(struct link_map* l, ElfW(Addr) reladdr,
  32. int relsize) {
  33. if (!l->l_info[DT_SYMTAB])
  34. return;
  35. ElfW(Rel)* r = (void*)reladdr, *end = (void*)(reladdr + relsize);
  36. ElfW(Sym)* symtab = (void*)D_PTR(l->l_info[DT_SYMTAB]);
  37. ElfW(Word) nrelative = l->l_info[RELCOUNT_IDX] ? l->l_info[RELCOUNT_IDX]->d_un.d_val : 0;
  38. ElfW(Rel)* relative = r;
  39. r = r + MIN(nrelative, relsize / sizeof(ElfW(Rel)));
  40. #ifndef RTLD_BOOTSTRAP
  41. /* This is defined in rtld.c, but nowhere in the static libc.a; make
  42. the reference weak so static programs can still link. This
  43. declaration cannot be done when compiling rtld.c (i.e. #ifdef
  44. RTLD_BOOTSTRAP) because rtld.c contains the common defn for
  45. _dl_rtld_map, which is incompatible with a weak decl in the same
  46. file. */
  47. #if !defined DO_RELA || defined ELF_MACHINE_REL_RELATIVE
  48. /* Rela platforms get the offset from r_addend and this must
  49. be copied in the relocation address. Therefore we can skip
  50. the relative relocations only if this is for rel
  51. relocations or rela relocations if they are computed as
  52. memory_loc += l_addr... */
  53. if (l->l_addr != 0)
  54. #else
  55. /* ...or we know the object has been prelinked. */
  56. if (l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
  57. #endif
  58. #endif
  59. for (; relative < r; ++relative) {
  60. DO_ELF_MACHINE_REL_RELATIVE(l, relative);
  61. }
  62. for (; r < end; ++r) {
  63. elf_machine_rel(l, r, &symtab[ELFW(R_SYM)(r->r_info)], (void*)(l->l_addr + r->r_offset));
  64. }
  65. }
  66. #undef elf_dynamic_do_rel
  67. #undef Rel
  68. #undef elf_machine_rel
  69. #undef elf_machine_rel_relative
  70. #undef DO_ELF_MACHINE_REL_RELATIVE
  71. #undef RELCOUNT_IDX