X-Git-Url: https://ruderich.org/simon/gitweb/?a=blobdiff_plain;f=src%2Fldpreload.h;h=12c6b16e1be24d37d0e1e3506fd158f946bab0ec;hb=HEAD;hp=a3d646e89a00eaaa823149a7a4f87f7dbf9acc7f;hpb=dfd317650f05542db49d7bc37649a232f28efa3f;p=coloredstderr%2Fcoloredstderr.git diff --git a/src/ldpreload.h b/src/ldpreload.h index a3d646e..12c6b16 100644 --- a/src/ldpreload.h +++ b/src/ldpreload.h @@ -2,7 +2,7 @@ * Helper header for LD_PRELOAD related headers macros. Must be loaded _first_ * (for RTLD_NEXT)! * - * Copyright (C) 2012-2013 Simon Ruderich + * Copyright (C) 2012-2018 Simon Ruderich * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -28,24 +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)) { \ - int saved_errnox = errno; \ - 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(); \ - } \ - errno = saved_errnox; \ + if (unlikely(!(pointer))) { \ + *(void **) (&(pointer)) = dlsym_function(name); \ } #endif