blob: 04026fcaa7f11a4d5f3eb1d689aab85fe7f94f71 [file] [log] [blame]
Elliott Hughes7c10abb2017-04-21 17:15:41 -07001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#include <gtest/gtest.h>
30
Ryan Pricharda2e83ab2019-08-16 17:25:43 -070031#include <dlfcn.h>
Elliott Hughes7c10abb2017-04-21 17:15:41 -070032#include <link.h>
Ryan Prichardc20f9a52018-08-21 18:34:21 -070033#if __has_include(<sys/auxv.h>)
34#include <sys/auxv.h>
35#endif
36
37#include <string>
38#include <unordered_map>
Elliott Hughes7c10abb2017-04-21 17:15:41 -070039
Elliott Hughes00a07c72025-04-17 15:27:29 -040040extern "C" void* __executable_start;
41
Elliott Hughes7c10abb2017-04-21 17:15:41 -070042TEST(link, dl_iterate_phdr_early_exit) {
43 static size_t call_count = 0;
44 ASSERT_EQ(123, dl_iterate_phdr([](dl_phdr_info*, size_t, void*) { ++call_count; return 123; },
45 nullptr));
46 ASSERT_EQ(1u, call_count);
47}
48
49TEST(link, dl_iterate_phdr) {
50 struct Functor {
51 static int Callback(dl_phdr_info* i, size_t s, void* data) {
Ryan Prichardc20f9a52018-08-21 18:34:21 -070052 static_cast<Functor*>(data)->DoChecks(i, s);
Elliott Hughes7c10abb2017-04-21 17:15:41 -070053 return 0;
54 }
55 void DoChecks(dl_phdr_info* info, size_t s) {
56 ASSERT_EQ(sizeof(dl_phdr_info), s);
57
Elliott Hughes7c10abb2017-04-21 17:15:41 -070058 ASSERT_TRUE(info->dlpi_name != nullptr);
59
Ryan Prichardc20f9a52018-08-21 18:34:21 -070060 // An ELF file must have at least a PT_LOAD program header.
61 ASSERT_NE(nullptr, info->dlpi_phdr);
62 ASSERT_NE(0, info->dlpi_phnum);
63
Elliott Hughes7c10abb2017-04-21 17:15:41 -070064 // Find the first PT_LOAD program header so we can find the ELF header.
Ryan Prichardc20f9a52018-08-21 18:34:21 -070065 bool found_load = false;
Elliott Hughes7c10abb2017-04-21 17:15:41 -070066 for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) {
67 const ElfW(Phdr)* phdr = reinterpret_cast<const ElfW(Phdr)*>(&info->dlpi_phdr[i]);
68 if (phdr->p_type == PT_LOAD) {
69 const ElfW(Ehdr)* ehdr = reinterpret_cast<const ElfW(Ehdr)*>(info->dlpi_addr +
70 phdr->p_vaddr);
71 // Does it look like an ELF file?
72 ASSERT_EQ(0, memcmp(ehdr, ELFMAG, SELFMAG));
73 // Does the e_phnum match what dl_iterate_phdr told us?
74 ASSERT_EQ(info->dlpi_phnum, ehdr->e_phnum);
Ryan Prichardc20f9a52018-08-21 18:34:21 -070075 found_load = true;
Elliott Hughes7c10abb2017-04-21 17:15:41 -070076 break;
77 }
78 }
Ryan Prichardc20f9a52018-08-21 18:34:21 -070079 ASSERT_EQ(true, found_load);
Elliott Hughes7c10abb2017-04-21 17:15:41 -070080 }
81 size_t count;
82 } f = {};
83 ASSERT_EQ(0, dl_iterate_phdr(Functor::Callback, &f));
84}
85
Ryan Pricharda2e83ab2019-08-16 17:25:43 -070086// Verify that the module load/unload counters from dl_iterate_phdr are incremented.
87TEST(link, dl_iterate_phdr_counters) {
88 struct Counters {
89 bool inited = false;
90 uint64_t adds = 0;
91 uint64_t subs = 0;
92 };
93
94 auto get_adds_subs = []() {
95 auto callback = [](dl_phdr_info* info, size_t size, void* data) {
96 Counters& counters = *static_cast<Counters*>(data);
97 EXPECT_GE(size, sizeof(dl_phdr_info));
98 if (!counters.inited) {
99 counters.inited = true;
100 counters.adds = info->dlpi_adds;
101 counters.subs = info->dlpi_subs;
102 } else {
103 // The counters have the same value for each module.
104 EXPECT_EQ(counters.adds, info->dlpi_adds);
105 EXPECT_EQ(counters.subs, info->dlpi_subs);
106 }
107 return 0;
108 };
109
110 Counters counters {};
111 EXPECT_EQ(0, dl_iterate_phdr(callback, &counters));
112 EXPECT_TRUE(counters.inited);
113 return counters;
114 };
115
116 // dlopen increments the 'adds' counter.
117 const auto before_dlopen = get_adds_subs();
118 void* const handle = dlopen("libtest_empty.so", RTLD_NOW);
119 ASSERT_NE(nullptr, handle);
120 const auto after_dlopen = get_adds_subs();
121 ASSERT_LT(before_dlopen.adds, after_dlopen.adds);
122 ASSERT_EQ(before_dlopen.subs, after_dlopen.subs);
123
124 // dlclose increments the 'subs' counter.
125 const auto before_dlclose = after_dlopen;
126 dlclose(handle);
127 const auto after_dlclose = get_adds_subs();
128 ASSERT_EQ(before_dlclose.adds, after_dlclose.adds);
129 ASSERT_LT(before_dlclose.subs, after_dlclose.subs);
130}
131
Ryan Prichardc20f9a52018-08-21 18:34:21 -0700132struct ProgHdr {
133 const ElfW(Phdr)* table;
134 size_t size;
135};
136
137__attribute__((__unused__))
138static ElfW(Addr) find_exe_load_bias(const ProgHdr& phdr) {
139 for (size_t i = 0; i < phdr.size; ++i) {
140 if (phdr.table[i].p_type == PT_PHDR) {
141 return reinterpret_cast<ElfW(Addr)>(phdr.table) - phdr.table[i].p_vaddr;
142 }
143 }
144 return 0;
145}
146
147__attribute__((__unused__))
148static ElfW(Dyn)* find_dynamic(const ProgHdr& phdr, ElfW(Addr) load_bias) {
149 for (size_t i = 0; i < phdr.size; ++i) {
150 if (phdr.table[i].p_type == PT_DYNAMIC) {
151 return reinterpret_cast<ElfW(Dyn)*>(phdr.table[i].p_vaddr + load_bias);
152 }
153 }
154 return nullptr;
155}
156
157__attribute__((__unused__))
158static r_debug* find_exe_r_debug(ElfW(Dyn)* dynamic) {
159 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
160 if (d->d_tag == DT_DEBUG) {
161 return reinterpret_cast<r_debug*>(d->d_un.d_val);
162 }
163 }
164 return nullptr;
165}
166
Elliott Hughes00a07c72025-04-17 15:27:29 -0400167TEST(link, dl_iterate_phdr_order) {
168 struct Object {
169 std::string name;
170 void* addr;
171 };
172 auto callback = [](dl_phdr_info* info, size_t, void* data) {
173 std::vector<Object>& names = *static_cast<std::vector<Object>*>(data);
174 names.push_back(Object{info->dlpi_name ?: "(null)", reinterpret_cast<void*>(info->dlpi_addr)});
175 return 0;
176 };
177 std::vector<Object> objects;
178 ASSERT_EQ(0, dl_iterate_phdr(callback, &objects));
179
180 // The executable should come first.
181 ASSERT_TRUE(!objects.empty());
182 ASSERT_EQ(&__executable_start, objects[0].addr) << objects[0].name;
183}
184
Ryan Prichardc20f9a52018-08-21 18:34:21 -0700185// Walk the DT_DEBUG/_r_debug global module list and compare it with the same
186// information from dl_iterate_phdr. Verify that the executable appears first
187// in _r_debug.
188TEST(link, r_debug) {
189#if __has_include(<sys/auxv.h>)
190 // Find the executable's PT_DYNAMIC segment and DT_DEBUG value. The linker
191 // will write the address of its _r_debug global into the .dynamic section.
192 ProgHdr exe_phdr = {
193 .table = reinterpret_cast<ElfW(Phdr)*>(getauxval(AT_PHDR)),
194 .size = getauxval(AT_PHNUM)
195 };
196 ASSERT_NE(nullptr, exe_phdr.table);
197 ElfW(Addr) exe_load_bias = find_exe_load_bias(exe_phdr);
198 ASSERT_NE(0u, exe_load_bias);
199 ElfW(Dyn)* exe_dynamic = find_dynamic(exe_phdr, exe_load_bias);
200 ASSERT_NE(nullptr, exe_dynamic);
201 r_debug* dbg = find_exe_r_debug(exe_dynamic);
202 ASSERT_NE(nullptr, dbg);
203
204 // Use dl_iterate_phdr to build a table mapping from load bias values to
205 // solib names and PT_DYNAMIC segments.
206 struct DlIterateInfo {
207 std::string name;
208 ElfW(Dyn)* dynamic;
209 };
210 struct Functor {
211 std::unordered_map<ElfW(Addr), DlIterateInfo> dl_iter_mods;
212 static int Callback(dl_phdr_info* i, size_t s, void* data) {
213 static_cast<Functor*>(data)->AddModule(i, s);
214 return 0;
215 }
216 void AddModule(dl_phdr_info* info, size_t s) {
217 ASSERT_EQ(sizeof(dl_phdr_info), s);
Elliott Hughesf3724772024-06-25 11:18:09 +0000218 ASSERT_FALSE(dl_iter_mods.contains(info->dlpi_addr));
Ryan Prichardc20f9a52018-08-21 18:34:21 -0700219 ASSERT_TRUE(info->dlpi_name != nullptr);
220 dl_iter_mods[info->dlpi_addr] = {
221 .name = info->dlpi_name,
222 .dynamic = find_dynamic({ info->dlpi_phdr, info->dlpi_phnum }, info->dlpi_addr)
223 };
224 }
225 } f = {};
226 ASSERT_EQ(0, dl_iterate_phdr(Functor::Callback, &f));
227
228 size_t map_size = 0;
229
230 for (link_map* map = dbg->r_map; map != nullptr; map = map->l_next) {
231 ASSERT_NE(0u, map->l_addr);
232 ASSERT_NE(nullptr, map->l_ld);
233 ASSERT_NE(nullptr, map->l_name);
234
235 auto it = f.dl_iter_mods.find(map->l_addr);
236 ASSERT_TRUE(it != f.dl_iter_mods.end());
237 const DlIterateInfo& info = it->second;
238 ASSERT_EQ(info.name, map->l_name);
239 ASSERT_EQ(info.dynamic, map->l_ld);
240
241 ++map_size;
242 }
243
244 // _r_debug and dl_iterate_phdr should report the same set of modules. We
245 // verified above that every _r_debug module was reported by dl_iterate_phdr,
246 // so checking the sizes verifies the converse.
247 ASSERT_EQ(f.dl_iter_mods.size(), map_size);
248
249 // Make sure the first entry is the executable. gdbserver assumes this and
250 // removes the first entry from its list of shared objects that it sends back
251 // to gdb.
252 ASSERT_EQ(exe_load_bias, dbg->r_map->l_addr);
253 ASSERT_EQ(exe_dynamic, dbg->r_map->l_ld);
254#endif
255}
256
Elliott Hughes7c10abb2017-04-21 17:15:41 -0700257#if __arm__
258static uintptr_t read_exidx_func(uintptr_t* entry) {
259 int32_t offset = *entry;
260 // Sign-extend from int31 to int32.
261 if ((offset & 0x40000000) != 0) {
262 offset += -0x7fffffff - 1;
263 }
264 return reinterpret_cast<uintptr_t>(entry) + offset;
265}
266__attribute__((__unused__)) static void another_function_in_same_ELF_file() {}
267#endif
268
269TEST(link, dl_unwind_find_exidx) {
270#if __arm__
271 int count = 0;
272 struct eit_entry_t {
273 uintptr_t one;
274 uintptr_t two;
275 };
276 eit_entry_t* entries = reinterpret_cast<eit_entry_t*>(dl_unwind_find_exidx(
277 reinterpret_cast<_Unwind_Ptr>(read_exidx_func), &count));
278 ASSERT_TRUE(entries != nullptr);
279 ASSERT_GT(count, 0);
280
Elliott Hughes68ae6ad2020-07-21 16:11:30 -0700281 // Validity checks.
Elliott Hughes7c10abb2017-04-21 17:15:41 -0700282 uintptr_t func = reinterpret_cast<uintptr_t>(read_exidx_func);
283 bool found = false;
284 for (int i = 0; i < count; ++i) {
285 // Entries must have bit 31 clear.
286 ASSERT_TRUE((entries[i].one & (1<<31)) == 0);
287
288 uintptr_t exidx_func = read_exidx_func(&entries[i].one);
289
290 // If our function is compiled for thumb, exception table contains our address - 1.
291 if (func == exidx_func || func == exidx_func + 1) found = true;
292
293 // Entries must be sorted. Some addresses may appear twice if function
294 // is compiled for arm.
295 if (i > 0) {
296 EXPECT_GE(exidx_func, read_exidx_func(&entries[i - 1].one)) << i;
297 }
298 }
299 ASSERT_TRUE(found);
300#else
Elliott Hughesbcaa4542019-03-08 15:20:23 -0800301 GTEST_SKIP() << "dl_unwind_find_exidx is an ARM-only API";
Elliott Hughes7c10abb2017-04-21 17:15:41 -0700302#endif
303}