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