X-Git-Url: https://ruderich.org/simon/gitweb/?p=coloredstderr%2Fcoloredstderr.git;a=blobdiff_plain;f=src%2Fldpreload.h;h=792679bb91d4fbf26ec17c18b239aa2e91f08979;hp=33a2e752d6e775bb03fbd3902ca2918f924475cc;hb=7eb1667d410a7b3836542460f3b069b0866d7dab;hpb=abc3d7889f3774717baf5795ffab2efb396d2570 diff --git a/src/ldpreload.h b/src/ldpreload.h index 33a2e75..792679b 100644 --- a/src/ldpreload.h +++ b/src/ldpreload.h @@ -28,22 +28,33 @@ #include /* dlsym() */ #include +#include +static void *dlsym_function(char const *name) noinline; +/* Load the function name using dlsym() and return it. Terminate program on + * failure. Split in function and macro to reduce code inserted into the + * function using the macro. */ +static void *dlsym_function(char const *name) { + void *result; + + int saved_errno = errno; /* just in case */ + + /* Clear possibly existing error. */ + dlerror(); + result = dlsym(RTLD_NEXT, name); + /* Not much we can do. Most likely the other output functions failed to + * load too. */ + if (dlerror() != NULL) { + abort(); + } + + errno = saved_errno; + return result; +} -/* Load the function name using dlsym() if necessary and store it in pointer. - * Terminate program on failure. */ #define DLSYM_FUNCTION(pointer, name) \ - if (NULL == (pointer)) { \ - char *error; \ - dlerror(); /* Clear possibly existing error. */ \ - \ - *(void **) (&(pointer)) = dlsym(RTLD_NEXT, (name)); \ - \ - if (NULL != (error = dlerror())) { \ - /* Not much we can do. Most likely the other output functions \ - * failed to load too. */ \ - abort(); \ - } \ + if (unlikely(!(pointer))) { \ + *(void **) (&(pointer)) = dlsym_function(name); \ } #endif