// An ELF file must have at least a PT_LOAD program header.
ASSERT_NE(nullptr, info->dlpi_phdr);
ASSERT_NE(0, info->dlpi_phnum);
// Find the first PT_LOAD program header so we can find the ELF header. bool found_load = false; for (ElfW(Half) i = 0; i < info->dlpi_phnum; ++i) { const ElfW(Phdr)* phdr = reinterpret_cast<const ElfW(Phdr)*>(&info->dlpi_phdr[i]); if (phdr->p_type == PT_LOAD) { const ElfW(Ehdr)* ehdr = reinterpret_cast<const ElfW(Ehdr)*>(info->dlpi_addr +
phdr->p_vaddr); // Does it look like an ELF file?
ASSERT_EQ(0, memcmp(ehdr, ELFMAG, SELFMAG)); // Does the e_phnum match what dl_iterate_phdr told us?
ASSERT_EQ(info->dlpi_phnum, ehdr->e_phnum);
found_load = true; break;
}
}
ASSERT_EQ(true, found_load);
}
size_t count;
} f = {};
ASSERT_EQ(0, dl_iterate_phdr(Functor::Callback, &f));
}
// Verify that the module load/unload counters from dl_iterate_phdr are incremented.
TEST(link, dl_iterate_phdr_counters) { struct Counters { bool inited = false;
uint64_t adds = 0;
uint64_t subs = 0;
};
auto get_adds_subs = []() { auto callback = [](dl_phdr_info* info, size_t size, void* data) {
Counters& counters = *static_cast<Counters*>(data);
EXPECT_GE(size, sizeof(dl_phdr_info)); if (!counters.inited) {
counters.inited = true;
counters.adds = info->dlpi_adds;
counters.subs = info->dlpi_subs;
} else { // The counters have the same value for each module.
EXPECT_EQ(counters.adds, info->dlpi_adds);
EXPECT_EQ(counters.subs, info->dlpi_subs);
} return0;
};
// The executable should come first.
ASSERT_TRUE(!objects.empty());
ASSERT_EQ(&__executable_start, objects[0].addr) << objects[0].name;
}
// Walk the DT_DEBUG/_r_debug global module list and compare it with the same // information from dl_iterate_phdr. Verify that the executable appears first // in _r_debug.
TEST(link, r_debug) { #if __has_include(<sys/auxv.h>) // Find the executable's PT_DYNAMIC segment and DT_DEBUG value. The linker // will write the address of its _r_debug global into the .dynamic section.
ProgHdr exe_phdr = {
.table = reinterpret_cast<ElfW(Phdr)*>(getauxval(AT_PHDR)),
.size = getauxval(AT_PHNUM)
};
ASSERT_NE(nullptr, exe_phdr.table);
ElfW(Addr) exe_load_bias = find_exe_load_bias(exe_phdr);
ASSERT_NE(0u, exe_load_bias);
ElfW(Dyn)* exe_dynamic = find_dynamic(exe_phdr, exe_load_bias);
ASSERT_NE(nullptr, exe_dynamic);
r_debug* dbg = find_exe_r_debug(exe_dynamic);
ASSERT_NE(nullptr, dbg);
// Use dl_iterate_phdr to build a table mapping from load bias values to // solib names and PT_DYNAMIC segments. struct DlIterateInfo {
std::string name;
ElfW(Dyn)* dynamic;
}; struct Functor {
std::unordered_map<ElfW(Addr), DlIterateInfo> dl_iter_mods; staticint Callback(dl_phdr_info* i, size_t s, void* data) { static_cast<Functor*>(data)->AddModule(i, s); return0;
} void AddModule(dl_phdr_info* info, size_t s) {
ASSERT_EQ(sizeof(dl_phdr_info), s);
ASSERT_FALSE(dl_iter_mods.contains(info->dlpi_addr));
ASSERT_TRUE(info->dlpi_name != nullptr);
dl_iter_mods[info->dlpi_addr] = {
.name = info->dlpi_name,
.dynamic = find_dynamic({ info->dlpi_phdr, info->dlpi_phnum }, info->dlpi_addr)
};
}
} f = {};
ASSERT_EQ(0, dl_iterate_phdr(Functor::Callback, &f));
auto it = f.dl_iter_mods.find(map->l_addr);
ASSERT_TRUE(it != f.dl_iter_mods.end()); const DlIterateInfo& info = it->second;
ASSERT_EQ(info.name, map->l_name);
ASSERT_EQ(info.dynamic, map->l_ld);
++map_size;
}
// _r_debug and dl_iterate_phdr should report the same set of modules. We // verified above that every _r_debug module was reported by dl_iterate_phdr, // so checking the sizes verifies the converse.
ASSERT_EQ(f.dl_iter_mods.size(), map_size);
// Make sure the first entry is the executable. gdbserver assumes this and // removes the first entry from its list of shared objects that it sends back // to gdb.
ASSERT_EQ(exe_load_bias, dbg->r_map->l_addr);
ASSERT_EQ(exe_dynamic, dbg->r_map->l_ld); #endif
}
// Validity checks.
uintptr_t func = reinterpret_cast<uintptr_t>(read_exidx_func); bool found = false; for (int i = 0; i < count; ++i) { // Entries must have bit 31 clear.
ASSERT_TRUE((entries[i].one & (1<<31)) == 0);
// If our function is compiled for thumb, exception table contains our address - 1. if (func == exidx_func || func == exidx_func + 1) found = true;
// Entries must be sorted. Some addresses may appear twice if function // is compiled for arm. if (i > 0) {
EXPECT_GE(exidx_func, read_exidx_func(&entries[i - 1].one)) << i;
}
}
ASSERT_TRUE(found); #else
GTEST_SKIP() << "dl_unwind_find_exidx is an ARM-only API"; #endif
}
Messung V0.5 in Prozent
¤ Dauer der Verarbeitung: 0.13 Sekunden
(vorverarbeitet am 2026-06-28)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.