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.
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.
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.
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/>. */
19 /* This file is included multiple times and therefore lacks a header
20 file inclusion guard. */
23 #include <libc-diag.h>
30 inline void __attribute__ ((unused
, always_inline
))
31 elf_get_dynamic_info (struct link_map
*l
, ElfW(Dyn
) *temp
)
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
;
39 #if !defined RTLD_BOOTSTRAP && !defined STATIC_PIE_BOOTSTRAP
44 ElfW(Dyn
) **info
= l
->l_info
;
46 for (ElfW(Dyn
) *dyn
= l
->l_ld
; dyn
->d_tag
!= DT_NULL
; dyn
++)
50 if ((d_tag_utype
) dyn
->d_tag
< DT_NUM
)
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
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
;
72 #define DL_RO_DYN_TEMP_CNT 8
74 #ifndef DL_RO_DYN_SECTION
75 /* Don't adjust .dynamic unnecessarily. */
78 ElfW(Addr
) l_addr
= l
->l_addr
;
81 # define ADJUST_DYN_INFO(tag) \
83 if (info[tag] != NULL) \
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++; \
92 info[tag]->d_un.d_ptr += l_addr; \
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
);
103 # if ! ELF_MACHINE_NO_REL
104 ADJUST_DYN_INFO (DT_REL
);
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
);
113 if (info
[DT_PLTREL
] != NULL
)
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
);
120 assert (info
[DT_PLTREL
]->d_un
.d_val
== DT_REL
121 || info
[DT_PLTREL
]->d_un
.d_val
== DT_RELA
);
124 #if ! ELF_MACHINE_NO_RELA
125 if (info
[DT_RELA
] != NULL
)
126 assert (info
[DT_RELAENT
]->d_un
.d_val
== sizeof (ElfW(Rela
)));
128 # if ! ELF_MACHINE_NO_REL
129 if (info
[DT_REL
] != NULL
)
130 assert (info
[DT_RELENT
]->d_un
.d_val
== sizeof (ElfW(Rel
)));
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);
140 #if defined RTLD_BOOTSTRAP || defined STATIC_PIE_BOOTSTRAP
141 assert (info
[DT_RUNPATH
] == NULL
);
142 assert (info
[DT_RPATH
] == NULL
);
144 if (info
[DT_FLAGS
] != NULL
)
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
;
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
];
158 if (info
[VERSYMIDX (DT_FLAGS_1
)] != NULL
)
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;
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
);
175 if (l
->l_flags_1
& DF_1_NOW
)
176 info
[DT_BIND_NOW
] = info
[VERSYMIDX (DT_FLAGS_1
)];
178 if (info
[DT_RUNPATH
] != NULL
)
179 /* If both RUNPATH and RPATH are given, the latter is ignored. */
180 info
[DT_RPATH
] = NULL
;