1 /* Inline functions for dynamic linking. 2 Copyright (C) 1995-2022 Free Software Foundation, Inc. 3 This file is part of the GNU C Library. 4 5 The GNU C Library is free software; you can redistribute it and/or 6 modify it under the terms of the GNU Lesser General Public 7 License as published by the Free Software Foundation; either 8 version 2.1 of the License, or (at your option) any later version. 9 10 The GNU C Library is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 13 Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public 16 License along with the GNU C Library; if not, see 17 <https://www.gnu.org/licenses/>. */ 18 19 #include <dl-machine.h> 20 #include <elf.h> 21 22 #ifdef RESOLVE_MAP 23 /* We pass reloc_addr as a pointer to void, as opposed to a pointer to 24 ElfW(Addr), because not all architectures can assume that the 25 relocated address is properly aligned, whereas the compiler is 26 entitled to assume that a pointer to a type is properly aligned for 27 the type. Even if we cast the pointer back to some other type with 28 less strict alignment requirements, the compiler might still 29 remember that the pointer was originally more aligned, thereby 30 optimizing away alignment tests or using word instructions for 31 copying memory, breaking the very code written to handle the 32 unaligned cases. */ 33 # if ! ELF_MACHINE_NO_REL 34 static inline void __attribute__((always_inline)) 35 elf_machine_rel (struct link_map *map, struct r_scope_elem *scope[], 36 const ElfW(Rel) *reloc, const ElfW(Sym) *sym, 37 const struct r_found_version *version, 38 void *const reloc_addr, int skip_ifunc); 39 static inline void __attribute__((always_inline)) 40 elf_machine_rel_relative (ElfW(Addr) l_addr, const ElfW(Rel) *reloc, 41 void *const reloc_addr); 42 # endif 43 # if ! ELF_MACHINE_NO_RELA 44 static inline void __attribute__((always_inline)) 45 elf_machine_rela (struct link_map *map, struct r_scope_elem *scope[], 46 const ElfW(Rela) *reloc, const ElfW(Sym) *sym, 47 const struct r_found_version *version, void *const reloc_addr, 48 int skip_ifunc); 49 static inline void __attribute__((always_inline)) 50 elf_machine_rela_relative (ElfW(Addr) l_addr, const ElfW(Rela) *reloc, 51 void *const reloc_addr); 52 # endif 53 # if ELF_MACHINE_NO_RELA || defined ELF_MACHINE_PLT_REL 54 static inline void __attribute__((always_inline)) 55 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[], 56 ElfW(Addr) l_addr, const ElfW(Rel) *reloc, 57 int skip_ifunc); 58 # else 59 static inline void __attribute__((always_inline)) 60 elf_machine_lazy_rel (struct link_map *map, struct r_scope_elem *scope[], 61 ElfW(Addr) l_addr, const ElfW(Rela) *reloc, 62 int skip_ifunc); 63 # endif 64 #endif 65 66 #ifdef RESOLVE_MAP 67 68 /* Get the definitions of `elf_dynamic_do_rel' and `elf_dynamic_do_rela'. 69 These functions are almost identical, so we use cpp magic to avoid 70 duplicating their code. It cannot be done in a more general function 71 because we must be able to completely inline. */ 72 73 /* On some machines, notably SPARC, DT_REL* includes DT_JMPREL in its 74 range. Note that according to the ELF spec, this is completely legal! 75 76 We are guarenteed that we have one of three situations. Either DT_JMPREL 77 comes immediately after DT_REL*, or there is overlap and DT_JMPREL 78 consumes precisely the very end of the DT_REL*, or DT_JMPREL and DT_REL* 79 are completely separate and there is a gap between them. */ 80 81 # define _ELF_DYNAMIC_DO_RELOC(RELOC, reloc, map, scope, do_lazy, skip_ifunc, test_rel) \ 82 do { \ 83 struct { ElfW(Addr) start, size; \ 84 __typeof (((ElfW(Dyn) *) 0)->d_un.d_val) nrelative; int lazy; } \ 85 ranges[2] = { { 0, 0, 0, 0 }, { 0, 0, 0, 0 } }; \ 86 \ 87 /* With DT_RELR, DT_RELA/DT_REL can have zero value. */ \ 88 if ((map)->l_info[DT_##RELOC] != NULL \ 89 && (map)->l_info[DT_##RELOC]->d_un.d_ptr != 0) \ 90 { \ 91 ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \ 92 ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \ 93 if (map->l_info[VERSYMIDX (DT_##RELOC##COUNT)] != NULL) \ 94 ranges[0].nrelative \ 95 = map->l_info[VERSYMIDX (DT_##RELOC##COUNT)]->d_un.d_val; \ 96 } \ 97 if ((map)->l_info[DT_PLTREL] \ 98 && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \ 99 { \ 100 ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \ 101 ElfW(Addr) size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \ 102 \ 103 if (ranges[0].start == 0) \ 104 ranges[0].start = start; \ 105 if (ranges[0].start + ranges[0].size == (start + size)) \ 106 ranges[0].size -= size; \ 107 if (!(do_lazy) \ 108 && (ranges[0].start + ranges[0].size) == start) \ 109 { \ 110 /* Combine processing the sections. */ \ 111 ranges[0].size += size; \ 112 } \ 113 else \ 114 { \ 115 ranges[1].start = start; \ 116 ranges[1].size = size; \ 117 ranges[1].lazy = (do_lazy); \ 118 } \ 119 } \ 120 \ 121 for (int ranges_index = 0; ranges_index < 2; ++ranges_index) \ 122 elf_dynamic_do_##reloc ((map), scope, \ 123 ranges[ranges_index].start, \ 124 ranges[ranges_index].size, \ 125 ranges[ranges_index].nrelative, \ 126 ranges[ranges_index].lazy, \ 127 skip_ifunc); \ 128 } while (0) 129 130 # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA 131 # define _ELF_CHECK_REL 0 132 # else 133 # define _ELF_CHECK_REL 1 134 # endif 135 136 # if ! ELF_MACHINE_NO_REL 137 # include "do-rel.h" 138 # define ELF_DYNAMIC_DO_REL(map, scope, lazy, skip_ifunc) \ 139 _ELF_DYNAMIC_DO_RELOC (REL, Rel, map, scope, lazy, skip_ifunc, _ELF_CHECK_REL) 140 # else 141 # define ELF_DYNAMIC_DO_REL(map, scope, lazy, skip_ifunc) /* Nothing to do. */ 142 # endif 143 144 # if ! ELF_MACHINE_NO_RELA 145 # define DO_RELA 146 # include "do-rel.h" 147 # define ELF_DYNAMIC_DO_RELA(map, scope, lazy, skip_ifunc) \ 148 _ELF_DYNAMIC_DO_RELOC (RELA, Rela, map, scope, lazy, skip_ifunc, _ELF_CHECK_REL) 149 # else 150 # define ELF_DYNAMIC_DO_RELA(map, scope, lazy, skip_ifunc) /* Nothing to do. */ 151 # endif 152 153 # define ELF_DYNAMIC_DO_RELR(map) \ 154 do { \ 155 ElfW(Addr) l_addr = (map)->l_addr, *where = 0; \ 156 const ElfW(Relr) *r, *end; \ 157 if ((map)->l_info[DT_RELR] == NULL) \ 158 break; \ 159 r = (const ElfW(Relr) *)D_PTR((map), l_info[DT_RELR]); \ 160 end = (const ElfW(Relr) *)((const char *)r + \ 161 (map)->l_info[DT_RELRSZ]->d_un.d_val); \ 162 for (; r < end; r++) \ 163 { \ 164 ElfW(Relr) entry = *r; \ 165 if ((entry & 1) == 0) \ 166 { \ 167 where = (ElfW(Addr) *)(l_addr + entry); \ 168 *where++ += l_addr; \ 169 } \ 170 else \ 171 { \ 172 for (long int i = 0; (entry >>= 1) != 0; i++) \ 173 if ((entry & 1) != 0) \ 174 where[i] += l_addr; \ 175 where += CHAR_BIT * sizeof(ElfW(Relr)) - 1; \ 176 } \ 177 } \ 178 } while (0); 179 180 /* This can't just be an inline function because GCC is too dumb 181 to inline functions containing inlines themselves. */ 182 # ifdef RTLD_BOOTSTRAP 183 # define DO_RTLD_BOOTSTRAP 1 184 # else 185 # define DO_RTLD_BOOTSTRAP 0 186 # endif 187 # define ELF_DYNAMIC_RELOCATE(map, scope, lazy, consider_profile, skip_ifunc) \ 188 do { \ 189 int edr_lazy = elf_machine_runtime_setup ((map), (scope), (lazy), \ 190 (consider_profile)); \ 191 if (((map) != &GL(dl_rtld_map) || DO_RTLD_BOOTSTRAP)) \ 192 ELF_DYNAMIC_DO_RELR (map); \ 193 ELF_DYNAMIC_DO_REL ((map), (scope), edr_lazy, skip_ifunc); \ 194 ELF_DYNAMIC_DO_RELA ((map), (scope), edr_lazy, skip_ifunc); \ 195 } while (0) 196 197 #endif 198