Skip to content

Instantly share code, notes, and snippets.

@SoniEx2
Last active August 29, 2015 14:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SoniEx2/32ba4caf50c96ad0d9bc to your computer and use it in GitHub Desktop.
Save SoniEx2/32ba4caf50c96ad0d9bc to your computer and use it in GitHub Desktop.

[ THIS IS A WIP AND MAY CHANGE AT ANY TIME! ]

Let's talk about shared objects and my "namespace" idea.

Let's say we have

void *dlnewns(void);
void *dlcopyns(void *namespace);
void *dlthisns(void);
void *dlnsopen(void *namespace, const char *filename, int flag);
void *dlnssym(void *namespace, const char *symbol);
int dlnsclose(void *namespace, void *handle);
int dldelns(void *namespace);

(If you notice, I didn't include a dlnserror(), we can just use dlerror() for that)

dlnewns() creates a new empty namespace and returns it. This is a completely isolated namespace.

dlcopyns() makes a copy of a namespace and returns that copy. This technically reopens all the handles inside the namespace, so the copy is isolated from the original namespace. (This lets libdl tweak all the handles/libs to work with the new namespace)

dlthisns() returns the namespace of the caller. This is mainly for use with dlcopyns()?

dlnsopen() is the namespaced variant of dlopen(). Returns a handle. RTLD_LOCAL only opens the lib with the namespace, without injecting the names into it. RTLD_GLOBAL injects names into the namespace.

dlnssym() is basically how you get things in to and out of namespaces. This does a lookup directly on the namespace, not on a handle.

dlnsclose() is the namespaced variant of dlclose(). Returns 0 on success and nonzero on error. (is this needed? can't we just use dlclose() instead?)

dldelns() is like dlclose(), but for namespaces. Because namespaces are local to the running application, not a shared resource, this actually destroys them. (Altho I guess if you pass a namespace into a namespace this would be a footgun... Meh, no worse than the dlopen() without dlclose() footgun)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment