| 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 | if ((map)->l_info[DT_##RELOC]) \ |
| 88 | { \ |
| 89 | ranges[0].start = D_PTR ((map), l_info[DT_##RELOC]); \ |
| 90 | ranges[0].size = (map)->l_info[DT_##RELOC##SZ]->d_un.d_val; \ |
| 91 | if (map->l_info[VERSYMIDX (DT_##RELOC##COUNT)] != NULL) \ |
| 92 | ranges[0].nrelative \ |
| 93 | = map->l_info[VERSYMIDX (DT_##RELOC##COUNT)]->d_un.d_val; \ |
| 94 | } \ |
| 95 | if ((map)->l_info[DT_PLTREL] \ |
| 96 | && (!test_rel || (map)->l_info[DT_PLTREL]->d_un.d_val == DT_##RELOC)) \ |
| 97 | { \ |
| 98 | ElfW(Addr) start = D_PTR ((map), l_info[DT_JMPREL]); \ |
| 99 | ElfW(Addr) size = (map)->l_info[DT_PLTRELSZ]->d_un.d_val; \ |
| 100 | \ |
| 101 | if (ranges[0].start + ranges[0].size == (start + size)) \ |
| 102 | ranges[0].size -= size; \ |
| 103 | if (!(do_lazy) \ |
| 104 | && (ranges[0].start + ranges[0].size) == start) \ |
| 105 | { \ |
| 106 | /* Combine processing the sections. */ \ |
| 107 | ranges[0].size += size; \ |
| 108 | } \ |
| 109 | else \ |
| 110 | { \ |
| 111 | ranges[1].start = start; \ |
| 112 | ranges[1].size = size; \ |
| 113 | ranges[1].lazy = (do_lazy); \ |
| 114 | } \ |
| 115 | } \ |
| 116 | \ |
| 117 | for (int ranges_index = 0; ranges_index < 2; ++ranges_index) \ |
| 118 | elf_dynamic_do_##reloc ((map), scope, \ |
| 119 | ranges[ranges_index].start, \ |
| 120 | ranges[ranges_index].size, \ |
| 121 | ranges[ranges_index].nrelative, \ |
| 122 | ranges[ranges_index].lazy, \ |
| 123 | skip_ifunc); \ |
| 124 | } while (0) |
| 125 | |
| 126 | # if ELF_MACHINE_NO_REL || ELF_MACHINE_NO_RELA |
| 127 | # define _ELF_CHECK_REL 0 |
| 128 | # else |
| 129 | # define _ELF_CHECK_REL 1 |
| 130 | # endif |
| 131 | |
| 132 | # if ! ELF_MACHINE_NO_REL |
| 133 | # include "do-rel.h" |
| 134 | # define ELF_DYNAMIC_DO_REL(map, scope, lazy, skip_ifunc) \ |
| 135 | _ELF_DYNAMIC_DO_RELOC (REL, Rel, map, scope, lazy, skip_ifunc, _ELF_CHECK_REL) |
| 136 | # else |
| 137 | # define ELF_DYNAMIC_DO_REL(map, scope, lazy, skip_ifunc) /* Nothing to do. */ |
| 138 | # endif |
| 139 | |
| 140 | # if ! ELF_MACHINE_NO_RELA |
| 141 | # define DO_RELA |
| 142 | # include "do-rel.h" |
| 143 | # define ELF_DYNAMIC_DO_RELA(map, scope, lazy, skip_ifunc) \ |
| 144 | _ELF_DYNAMIC_DO_RELOC (RELA, Rela, map, scope, lazy, skip_ifunc, _ELF_CHECK_REL) |
| 145 | # else |
| 146 | # define ELF_DYNAMIC_DO_RELA(map, scope, lazy, skip_ifunc) /* Nothing to do. */ |
| 147 | # endif |
| 148 | |
| 149 | /* This can't just be an inline function because GCC is too dumb |
| 150 | to inline functions containing inlines themselves. */ |
| 151 | # define ELF_DYNAMIC_RELOCATE(map, scope, lazy, consider_profile, skip_ifunc) \ |
| 152 | do { \ |
| 153 | int edr_lazy = elf_machine_runtime_setup ((map), (scope), (lazy), \ |
| 154 | (consider_profile)); \ |
| 155 | ELF_DYNAMIC_DO_REL ((map), (scope), edr_lazy, skip_ifunc); \ |
| 156 | ELF_DYNAMIC_DO_RELA ((map), (scope), edr_lazy, skip_ifunc); \ |
| 157 | } while (0) |
| 158 | |
| 159 | #endif |
| 160 | |