I am writing a server application in C that links to three shared libraries. One of the libraries returns NULL from functions that involve one or more malloc(3)'s that fail and the two other libraries call abort(3) from within a function if malloc fails. My application wraps malloc(3) in a function e.g. amalloc that checks for malloc(3) fails (returning NULL) and shuts down the server as gracefully as possible.
Am I able to override calls to malloc(3) in all of the shared libraries so that they use my wrapper function? I would like to be able to handle malloc(3) fails that occur in the shared libraries as well as my application. I would prefer to not have to recompile them but if that's necessary then I can do.
I am using gcc version 4.9.2 (Debian 4.9.2-10) and writing the server to run exclusively on Linux. The shared libraries are linked using -lx, -ly, -lz