do-rel.h 3.4 KB

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