do-rel.h 3.5 KB

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