Skip to content

Commit fdaf195

Browse files
authored
Detect and parse the loader in parseLibraries. (async-profiler#1263)
1 parent 047a6de commit fdaf195

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

src/symbols_linux.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include <fcntl.h>
2121
#include <link.h>
2222
#include <linux/limits.h>
23+
#include <sys/auxv.h>
2324
#include "symbols.h"
2425
#include "dwarf.h"
2526
#include "fdtransferClient.h"
@@ -775,6 +776,11 @@ void Symbols::parseLibraries(CodeCacheArray* array, bool kernel_symbols) {
775776
std::unordered_map<u64, SharedLibrary> libs;
776777
collectSharedLibraries(libs, MAX_NATIVE_LIBS - array->count());
777778

779+
const char* ld_base = (const char*)getauxval(AT_BASE);
780+
if (!ld_base) {
781+
Log::warn("Cannot determine base address of the loader");
782+
}
783+
778784
const void* main_phdr = NULL;
779785
dl_iterate_phdr([](struct dl_phdr_info* info, size_t size, void* data) {
780786
*(const void**)data = info->dlpi_phdr;
@@ -813,9 +819,11 @@ void Symbols::parseLibraries(CodeCacheArray* array, bool kernel_symbols) {
813819
// Main executable and ld-linux interpreter cannot be dlopen'ed, but dlerror() returns NULL for them on some systems.
814820
void* handle = dlopen(lib.file, RTLD_LAZY | RTLD_NOLOAD);
815821

816-
// Parse main executable regardless of dlopen result, since it cannot be unloaded.
822+
// Parse main executable and the loader (ld.so) regardless of dlopen result, since they cannot be unloaded.
817823
bool is_main_exe = main_phdr >= lib.image_base && main_phdr < lib.map_end;
818-
if (handle != NULL || dlerror() == NULL || is_main_exe) {
824+
bool is_loader = ld_base == lib.image_base;
825+
826+
if (handle != NULL || is_main_exe || is_loader) {
819827
ElfParser::parseProgramHeaders(cc, lib.image_base, lib.map_end, OS::isMusl());
820828
}
821829

0 commit comments

Comments
 (0)