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