What system calls does dlopen use?

When using the dlopen API - dlopen, dlsym, dlclose - what syscalls does it use? I wrote two programs to compare: one which loads a function from a shared object, and one which embeds the function in the main binary. Using dtruss to log system calls, the dynamic-loading version does something like this:

void* start = 0x10EA52000;
size_t pagesize = getpagesize();
struct stat64 stat;
stat64("plugin.so", &stat);
int fd = open("plugin.so", 0, 0);
mmap(start+(0*pagesize), 6*pagesize,               PROT_READ|PROT_EXEC,  MAP_FIXED|MAP_PRIVATE, fd, 0*pagesize);
mmap(start+(6*pagesize), 1*pagesize,               PROT_READ|PROT_WRITE, MAP_FIXED|MAP_PRIVATE, fd, 1*pagesize);
mmap(start+(7*pagesize), stat.off_t - 7*pagesize,  PROT_READ,            MAP_FIXED|MAP_PRIVATE, fd, 2*pagesize);
close(fd);
munmap(start+(0*pagesize), 4096);
munmap(start+(6*pagesize), 4096);
munmap(start+(7*pagesize), 4096);

It’s notable that this does not use any special syscalls. Instead, the program uses standard UNIX syscalls to put the file in memory.

The program knows the offsets of three sections of the file: one executable (containing functions), one read/writable (containing global variables), and one readable (the Mach-O headers, at the end of the file?). The program maps these into memory at fixed locations.

I just released Vidrio, a free app for macOS and Windows to make your screen-sharing awesomely holographic. Vidrio shows your webcam video on your screen, just like a mirror. Then you just share or record your screen with Zoom, QuickTime, or any other app. Vidrio makes your presentations effortlessly engaging, showing your gestures, gazes, and expressions. #1 on Product Hunt. Available for macOS and Windows.

With Vidrio

With generic competitor

More by Jim

Tagged . All content copyright James Fisher 2017. This post is not associated with my employer. Found an error? Edit this page.