Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcfa/src/interpose.cfa

    re10714a r9cd5bd2  
    1010// Created On       : Wed Mar 29 16:10:31 2017
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Mon Mar 27 21:09:03 2023
    13 // Update Count     : 196
    14 //
    15 
     12// Last Modified On : Fri Mar 13 17:35:37 2020
     13// Update Count     : 178
     14//
     15
     16#include <stdarg.h>                                                                             // va_start, va_end
    1617#include <stdio.h>
     18#include <string.h>                                                                             // strlen
    1719#include <unistd.h>                                                                             // _exit, getpid
     20#define __USE_GNU
     21#include <signal.h>
     22#undef __USE_GNU
    1823extern "C" {
    1924#include <dlfcn.h>                                                                              // dlopen, dlsym
     
    2126}
    2227
     28#include "bits/debug.hfa"
    2329#include "bits/defs.hfa"
    2430#include "bits/signal.hfa"                                                              // sigHandler_?
     
    3642
    3743typedef void (* generic_fptr_t)(void);
    38 
    3944static generic_fptr_t do_interpose_symbol( void * library, const char symbol[], const char version[] ) {
     45        const char * error;
     46
    4047        union { generic_fptr_t fptr; void * ptr; } originalFunc;
    4148
    4249        #if defined( _GNU_SOURCE )
    43         if ( version ) {
    44                 originalFunc.ptr = dlvsym( library, symbol, version );
    45         } else {
     50                if ( version ) {
     51                        originalFunc.ptr = dlvsym( library, symbol, version );
     52                } else {
     53                        originalFunc.ptr = dlsym( library, symbol );
     54                }
     55        #else
    4656                originalFunc.ptr = dlsym( library, symbol );
     57        #endif // _GNU_SOURCE
     58
     59        error = dlerror();
     60        if ( error ) abort( "interpose_symbol : internal error, %s\n", error );
     61
     62        return originalFunc.fptr;
     63}
     64
     65static generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) {
     66        const char * error;
     67
     68        static void * library;
     69        static void * pthread_library;
     70        if ( ! library ) {
     71                #if defined( RTLD_NEXT )
     72                        library = RTLD_NEXT;
     73                #else
     74                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
     75                        library = dlopen( "libc.so.6", RTLD_LAZY );
     76                        error = dlerror();
     77                        if ( error ) {
     78                                abort( "interpose_symbol : failed to open libc, %s\n", error );
     79                        }
     80                #endif
    4781        } // if
    48         #else
    49         originalFunc.ptr = dlsym( library, symbol );
    50         #endif // _GNU_SOURCE
    51 
    52         if ( ! originalFunc.ptr ) {                                                     // == nullptr
    53                 abort( "interpose_symbol : internal error, %s\n", dlerror() );
     82        if ( ! pthread_library ) {
     83                #if defined( RTLD_NEXT )
     84                        pthread_library = RTLD_NEXT;
     85                #else
     86                        // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
     87                        pthread_library = dlopen( "libpthread.so", RTLD_LAZY );
     88                        error = dlerror();
     89                        if ( error ) {
     90                                abort( "interpose_symbol : failed to open libpthread, %s\n", error );
     91                        }
     92                #endif
    5493        } // if
    55         return originalFunc.fptr;
    56 }
    57 
    58 static generic_fptr_t interpose_symbol( const char symbol[], const char version[] ) {
    59         void * library;
    60 
    61         #if defined( RTLD_NEXT )
    62         library = RTLD_NEXT;
    63         #else
    64         // missing RTLD_NEXT => must hard-code library name, assuming libstdc++
    65         library = dlopen( "libc.so.6", RTLD_LAZY );
    66         if ( ! library ) {                                                                      // == nullptr
    67                 abort( "interpose_symbol : failed to open libc, %s\n", dlerror() );
    68         } // if
    69         #endif // RTLD_NEXT
    70 
    71         return do_interpose_symbol( library, symbol, version );
     94
     95        return do_interpose_symbol(library, symbol, version);
    7296}
    7397
     
    99123                preload_libgcc();
    100124
    101                 #pragma GCC diagnostic push
    102                 #pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
     125#pragma GCC diagnostic push
     126#pragma GCC diagnostic ignored "-Wdiscarded-qualifiers"
    103127                INTERPOSE_LIBC( abort, version );
    104128                INTERPOSE_LIBC( exit , version );
    105                 #pragma GCC diagnostic pop
     129#pragma GCC diagnostic pop
    106130
    107131                if(__cfathreadabi_interpose_startup) __cfathreadabi_interpose_startup( do_interpose_symbol );
    108 
    109                 // SKULLDUGGERY: In Ubuntu 22.04, someone augmented signal.h to allow SIGSTKSZ to be "sysconf(_SC_SIGSTKSZ)" in
    110                 // sigstksz.h, as well as 8192 in sigstack.h. HOWEVER, they forgot to provide a mechanism to tell signal.h to
    111                 // use sigstack.h rather than sigstksz.h. (I'm not happy.) By undefining _GNU_SOURCE before signal.h and
    112                 // redefining it afterwards, you can get 8192, but then nothing works correctly inside of signal.h without
    113                 // _GNU_SOURCE defined.  So what is needed is a way to get signal.h to use sigstack.h WITH _GNU_SOURCE defined.
    114                 // Basically something is wrong with features.h and its use in signal.h.
    115 
    116                 #undef SIGSTKSZ
    117                 #define SIGSTKSZ 8192
    118132
    119133                // As a precaution (and necessity), errors that result in termination are delivered on a separate stack because
     
    281295        va_start( args, fmt );
    282296        __abort( false, fmt, args );
    283         // CONTROL NEVER REACHES HERE!
     297    // CONTROL NEVER REACHES HERE!
    284298        va_end( args );
    285299}
    286300
    287301void abort( bool signalAbort, const char fmt[], ... ) {
    288         va_list args;
    289         va_start( args, fmt );
    290         __abort( signalAbort, fmt, args );
    291         // CONTROL NEVER REACHES HERE!
    292         va_end( args );
     302    va_list args;
     303    va_start( args, fmt );
     304    __abort( signalAbort, fmt, args );
     305    // CONTROL NEVER REACHES HERE!
     306    va_end( args );
    293307}
    294308
Note: See TracChangeset for help on using the changeset viewer.