Skip to content

Unable to dynamically link librseq with dlopen() #22

@willowec

Description

@willowec

Hello,

I have been attempting to dynamically link librseq using libdl's dlopen() and dlsym() functions. While attempting to do so, I have encountered a fairly confusing error. The minimum example and output are shown below:

/* A minimum example of dlsym failing to link librseq */
#include <dlfcn.h>
#include <stdio.h>

/* function pointers */
static int (*rseq_available_ptr)(unsigned int query);

/* local wrappers for function pointers */
static int librseq_rseq_available(unsigned int query) { return (*rseq_available_ptr)(query); }

int link_librseq()
{
	void* lib = dlopen("librseq.so", RTLD_NOW);
	if (!lib) {
		printf("Failed to load librseq: %s\n", dlerror());
		return -1;
	}

	rseq_available_ptr = dlsym(lib, "rseq_available");
	if (!rseq_available_ptr) {
		printf("Failed to access symbol 'rseq_available': %s\n", dlerror());
		return -1;
	}

	return 0;
} 

int main()
{
	if (link_librseq() < 0)
		return 1;

	/* ensure rseq is here */
	if (librseq_rseq_available(0)) {
		printf("Rseq available!\n");
	} else {
		printf("The rseq syscall is unavailable");
	}

	return 0;
}

and the output when I execute it:

$ ./dlsym_err 
Failed to load librseq: /usr/local/lib/librseq.so: cannot allocate memory in static TLS block

It is my understanding from a significant amount of reading through various forums, and particularly this writeup by Chao-tic that this may be related to the per-thread variable __thread struct rseq_abi __rseq_abi. Do you have any tips on how I might be able to get librseq to load with dlopen()? I'm not even quite sure that this is the right place to ask, sorry.

One thing that is interesting is that using LD_PRELOAD makes this issue disappear. Of course, requiring that the user run their programs with LD_PRELOAD is kind of frustrating, so hopefully there is a way to work around that.

$ LD_PRELOAD=/usr/local/lib/librseq.so ./dlsym_err
Rseq available!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions