]>
Commit | Line | Data |
---|---|---|
9cee5585 | 1 | /* Read the dynamic section at DYN and fill in INFO with indices DT_*. |
6d7e8eda | 2 | Copyright (C) 2012-2023 Free Software Foundation, Inc. |
9cee5585 L |
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 | |
5a82c748 | 17 | <https://www.gnu.org/licenses/>. */ |
9cee5585 | 18 | |
490e6c62 FS |
19 | /* Populate dynamic tags in l_info. */ |
20 | ||
21 | #ifndef _GET_DYNAMIC_INFO_H | |
22 | #define _GET_DYNAMIC_INFO_H | |
9090848d | 23 | |
9cee5585 | 24 | #include <assert.h> |
d6d89608 | 25 | #include <dl-machine-rel.h> |
9090848d | 26 | #include <libc-diag.h> |
9cee5585 | 27 | |
490e6c62 | 28 | static inline void __attribute__ ((unused, always_inline)) |
5118dcac AZ |
29 | elf_get_dynamic_info (struct link_map *l, bool bootstrap, |
30 | bool static_pie_bootstrap) | |
9cee5585 | 31 | { |
9cee5585 L |
32 | #if __ELF_NATIVE_CLASS == 32 |
33 | typedef Elf32_Word d_tag_utype; | |
34 | #elif __ELF_NATIVE_CLASS == 64 | |
35 | typedef Elf64_Xword d_tag_utype; | |
36 | #endif | |
37 | ||
d6d89608 | 38 | #ifndef STATIC_PIE_BOOTSTRAP |
5118dcac | 39 | if (!bootstrap && l->l_ld == NULL) |
9cee5585 L |
40 | return; |
41 | #endif | |
42 | ||
86a4d3fa | 43 | ElfW(Dyn) **info = l->l_info; |
9cee5585 | 44 | |
86a4d3fa | 45 | for (ElfW(Dyn) *dyn = l->l_ld; dyn->d_tag != DT_NULL; dyn++) |
9cee5585 | 46 | { |
86a4d3fa SN |
47 | d_tag_utype i; |
48 | ||
9cee5585 | 49 | if ((d_tag_utype) dyn->d_tag < DT_NUM) |
86a4d3fa | 50 | i = dyn->d_tag; |
34a5a146 JM |
51 | else if (dyn->d_tag >= DT_LOPROC |
52 | && dyn->d_tag < DT_LOPROC + DT_THISPROCNUM) | |
86a4d3fa | 53 | i = dyn->d_tag - DT_LOPROC + DT_NUM; |
9cee5585 | 54 | else if ((d_tag_utype) DT_VERSIONTAGIDX (dyn->d_tag) < DT_VERSIONTAGNUM) |
86a4d3fa | 55 | i = VERSYMIDX (dyn->d_tag); |
9cee5585 | 56 | else if ((d_tag_utype) DT_EXTRATAGIDX (dyn->d_tag) < DT_EXTRANUM) |
86a4d3fa SN |
57 | i = DT_EXTRATAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM |
58 | + DT_VERSIONTAGNUM; | |
9cee5585 | 59 | else if ((d_tag_utype) DT_VALTAGIDX (dyn->d_tag) < DT_VALNUM) |
86a4d3fa SN |
60 | i = DT_VALTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM |
61 | + DT_VERSIONTAGNUM + DT_EXTRANUM; | |
9cee5585 | 62 | else if ((d_tag_utype) DT_ADDRTAGIDX (dyn->d_tag) < DT_ADDRNUM) |
86a4d3fa SN |
63 | i = DT_ADDRTAGIDX (dyn->d_tag) + DT_NUM + DT_THISPROCNUM |
64 | + DT_VERSIONTAGNUM + DT_EXTRANUM + DT_VALNUM; | |
65 | else | |
66 | continue; | |
67 | ||
68 | info[i] = dyn; | |
9cee5585 L |
69 | } |
70 | ||
9cee5585 | 71 | /* Don't adjust .dynamic unnecessarily. */ |
b413280c | 72 | if (l->l_addr != 0 && dl_relocate_ld (l)) |
9cee5585 L |
73 | { |
74 | ElfW(Addr) l_addr = l->l_addr; | |
9cee5585 L |
75 | |
76 | # define ADJUST_DYN_INFO(tag) \ | |
77 | do \ | |
60196d2e L |
78 | { \ |
79 | if (info[tag] != NULL) \ | |
80 | info[tag]->d_un.d_ptr += l_addr; \ | |
81 | } \ | |
9cee5585 L |
82 | while (0) |
83 | ||
84 | ADJUST_DYN_INFO (DT_HASH); | |
85 | ADJUST_DYN_INFO (DT_PLTGOT); | |
86 | ADJUST_DYN_INFO (DT_STRTAB); | |
87 | ADJUST_DYN_INFO (DT_SYMTAB); | |
60196d2e L |
88 | ADJUST_DYN_INFO (DT_RELR); |
89 | ADJUST_DYN_INFO (DT_JMPREL); | |
90 | ADJUST_DYN_INFO (VERSYMIDX (DT_VERSYM)); | |
91 | ADJUST_DYN_INFO (ADDRIDX (DT_GNU_HASH)); | |
92 | # undef ADJUST_DYN_INFO | |
93 | ||
94 | /* DT_RELA/DT_REL are mandatory. But they may have zero value if | |
95 | there is DT_RELR. Don't relocate them if they are zero. */ | |
96 | # define ADJUST_DYN_INFO(tag) \ | |
97 | do \ | |
98 | if (info[tag] != NULL && info[tag]->d_un.d_ptr != 0) \ | |
99 | info[tag]->d_un.d_ptr += l_addr; \ | |
100 | while (0) | |
101 | ||
9cee5585 L |
102 | # if ! ELF_MACHINE_NO_RELA |
103 | ADJUST_DYN_INFO (DT_RELA); | |
104 | # endif | |
105 | # if ! ELF_MACHINE_NO_REL | |
106 | ADJUST_DYN_INFO (DT_REL); | |
107 | # endif | |
9cee5585 | 108 | # undef ADJUST_DYN_INFO |
9cee5585 | 109 | } |
9cee5585 L |
110 | if (info[DT_PLTREL] != NULL) |
111 | { | |
112 | #if ELF_MACHINE_NO_RELA | |
113 | assert (info[DT_PLTREL]->d_un.d_val == DT_REL); | |
114 | #elif ELF_MACHINE_NO_REL | |
115 | assert (info[DT_PLTREL]->d_un.d_val == DT_RELA); | |
116 | #else | |
117 | assert (info[DT_PLTREL]->d_un.d_val == DT_REL | |
118 | || info[DT_PLTREL]->d_un.d_val == DT_RELA); | |
119 | #endif | |
120 | } | |
121 | #if ! ELF_MACHINE_NO_RELA | |
122 | if (info[DT_RELA] != NULL) | |
123 | assert (info[DT_RELAENT]->d_un.d_val == sizeof (ElfW(Rela))); | |
124 | # endif | |
125 | # if ! ELF_MACHINE_NO_REL | |
126 | if (info[DT_REL] != NULL) | |
127 | assert (info[DT_RELENT]->d_un.d_val == sizeof (ElfW(Rel))); | |
128 | #endif | |
e895cff5 FS |
129 | if (info[DT_RELR] != NULL) |
130 | assert (info[DT_RELRENT]->d_un.d_val == sizeof (ElfW(Relr))); | |
5118dcac | 131 | if (bootstrap || static_pie_bootstrap) |
9cee5585 | 132 | { |
5118dcac AZ |
133 | assert (info[DT_RUNPATH] == NULL); |
134 | assert (info[DT_RPATH] == NULL); | |
9cee5585 | 135 | } |
5118dcac | 136 | if (bootstrap) |
9cee5585 | 137 | { |
5118dcac AZ |
138 | /* Only the bind now flags are allowed. */ |
139 | assert (info[VERSYMIDX (DT_FLAGS_1)] == NULL | |
140 | || (info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val & ~DF_1_NOW) == 0); | |
141 | /* Flags must not be set for ld.so. */ | |
142 | assert (info[DT_FLAGS] == NULL | |
143 | || (info[DT_FLAGS]->d_un.d_val & ~DF_BIND_NOW) == 0); | |
9cee5585 | 144 | } |
5118dcac AZ |
145 | else |
146 | { | |
147 | if (info[DT_FLAGS] != NULL) | |
148 | { | |
149 | /* Flags are used. Translate to the old form where available. | |
150 | Since these l_info entries are only tested for NULL pointers it | |
151 | is ok if they point to the DT_FLAGS entry. */ | |
152 | l->l_flags = info[DT_FLAGS]->d_un.d_val; | |
153 | ||
154 | if (l->l_flags & DF_SYMBOLIC) | |
155 | info[DT_SYMBOLIC] = info[DT_FLAGS]; | |
156 | if (l->l_flags & DF_TEXTREL) | |
157 | info[DT_TEXTREL] = info[DT_FLAGS]; | |
158 | if (l->l_flags & DF_BIND_NOW) | |
159 | info[DT_BIND_NOW] = info[DT_FLAGS]; | |
160 | } | |
161 | ||
162 | if (info[VERSYMIDX (DT_FLAGS_1)] != NULL) | |
163 | { | |
164 | l->l_flags_1 = info[VERSYMIDX (DT_FLAGS_1)]->d_un.d_val; | |
165 | if (l->l_flags_1 & DF_1_NODELETE) | |
166 | l->l_nodelete_pending = true; | |
167 | ||
168 | /* Only DT_1_SUPPORTED_MASK bits are supported, and we would like | |
169 | to assert this, but we can't. Users have been setting | |
170 | unsupported DF_1_* flags for a long time and glibc has ignored | |
171 | them. Therefore to avoid breaking existing applications the | |
172 | best we can do is add a warning during debugging with the | |
173 | intent of notifying the user of the problem. */ | |
174 | if (__builtin_expect (GLRO(dl_debug_mask) & DL_DEBUG_FILES, 0) | |
175 | && l->l_flags_1 & ~DT_1_SUPPORTED_MASK) | |
176 | _dl_debug_printf ("\nWARNING: Unsupported flag value(s) of 0x%x " | |
177 | "in DT_FLAGS_1.\n", | |
178 | l->l_flags_1 & ~DT_1_SUPPORTED_MASK); | |
179 | ||
180 | if (l->l_flags_1 & DF_1_NOW) | |
181 | info[DT_BIND_NOW] = info[VERSYMIDX (DT_FLAGS_1)]; | |
182 | } | |
183 | ||
184 | if (info[DT_RUNPATH] != NULL) | |
185 | /* If both RUNPATH and RPATH are given, the latter is ignored. */ | |
186 | info[DT_RPATH] = NULL; | |
187 | } | |
9cee5585 | 188 | } |
490e6c62 FS |
189 | |
190 | #endif |