I'm trying to get a bit more information in __cxa_throw. Regardless of the wisdom of this, the following code works perfectly with gcc and from the limited information I can find about this, it should work its perfectly legal even if there are some caveats. Please note I'm only planning this for debug builds. Clang is giving
ld.lld: error: duplicate symbol: __cxa_throw
>>> defined at exc_override.cpp
>>> exc.o:(__cxa_throw)
>>> defined at cxa_exception.cpp
>>> cxa_exception.cpp.o:(.text.__cxa_throw+0x0) in archive /opt/compilers/clang-12.0.1/bin/../lib/x86_64-unknown-linux-gnu/c++/libc++.a
main.cxx
#include <iostream>
#include <stdexcept>
#include "exc_override.h"
int main(int argc, char** argn) {
try {
throw std::runtime_error("boom");
} catch(...)
{
std::cout << "caught" << std::endl;
}
return 0;
}
exc_override.h
#pragma once
extern "C"
void __cxa_throw (void *thrown_exception, void *pvtinfo, void (*dest)(void *)) ;
exc_override.cpp
#include "exc_override.h"
#include <cstdlib>
#include <dlfcn.h>
#include <execinfo.h>
#include "stdio.h"
typedef void (*cxa_throw_type)(void *, void *, void (*) (void *));
cxa_throw_type orig_cxa_throw = 0;
void load_orig_throw_code()
{
orig_cxa_throw = (cxa_throw_type) dlsym(RTLD_NEXT, "__cxa_throw");
}
extern "C"
void __cxa_throw (void *thrown_exception, void *pvtinfo, void (*dest)(void *)) {
printf(" ################ DETECT A THROWN !!!!! #############\n");
if (orig_cxa_throw == 0)
load_orig_throw_code();
{}
orig_cxa_throw(thrown_exception, pvtinfo, dest);
}
and I'm building it in gcc (4.8.5) with the following commands
gcc main.o exc_override.o -o testprogram
g++ main.o exc_override.o -o testprogram
g++ main.o exc_override.o -o testprogram -ldl >
In Clang 12.0.1
clang++ -c main.cxx -o main.o
clang++ -c exc_override.cpp -o exc.o
clang++ main.o exc.o -o testprogram