do-rel.h 3.4 KB

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