do-rel.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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, \
  30. (void *) (l->l_addr + relative->r_offset))
  31. #endif
  32. static void __attribute_unused
  33. elf_dynamic_do_rel (struct link_map *l, ElfW(Addr) reladdr, int relsize)
  34. {
  35. if (!l->l_info[DT_SYMTAB])
  36. return;
  37. ElfW(Rel) *r = (void *) reladdr, *end = (void *) (reladdr + relsize);
  38. ElfW(Sym) *symtab = (void *) D_PTR (l->l_info[DT_SYMTAB]);
  39. ElfW(Word) nrelative = l->l_info[RELCOUNT_IDX]? l->l_info[RELCOUNT_IDX]->d_un.d_val : 0;
  40. ElfW(Rel) *relative = r;
  41. r = r + MIN (nrelative, relsize / sizeof (ElfW(Rel)));
  42. #ifndef RTLD_BOOTSTRAP
  43. /* This is defined in rtld.c, but nowhere in the static libc.a; make
  44. the reference weak so static programs can still link. This
  45. declaration cannot be done when compiling rtld.c (i.e. #ifdef
  46. RTLD_BOOTSTRAP) because rtld.c contains the common defn for
  47. _dl_rtld_map, which is incompatible with a weak decl in the same
  48. file. */
  49. #if !defined DO_RELA || defined ELF_MACHINE_REL_RELATIVE
  50. /* Rela platforms get the offset from r_addend and this must
  51. be copied in the relocation address. Therefore we can skip
  52. the relative relocations only if this is for rel
  53. relocations or rela relocations if they are computed as
  54. memory_loc += l_addr... */
  55. if (l->l_addr != 0)
  56. #else
  57. /* ...or we know the object has been prelinked. */
  58. if (l->l_addr != 0 || !l->l_info[VALIDX(DT_GNU_PRELINKED)])
  59. #endif
  60. #endif
  61. for (; relative < r; ++relative)
  62. DO_ELF_MACHINE_REL_RELATIVE (l, relative);
  63. for (; r < end; ++r)
  64. elf_machine_rel (l, r, &symtab[ELFW(R_SYM) (r->r_info)],
  65. (void *) (l->l_addr + r->r_offset));
  66. }
  67. #undef elf_dynamic_do_rel
  68. #undef Rel
  69. #undef elf_machine_rel
  70. #undef elf_machine_rel_relative
  71. #undef DO_ELF_MACHINE_REL_RELATIVE
  72. #undef RELCOUNT_IDX