Pthread Stubs in C Library

shared library binding:
deferred
global hash table disabled
$ cc thread.c -lpthread
$ a.out
Success
$ chatr a.out
a.out:
shared executable
shared library dynamic path search:
SHLIB_PATH disabled second
embedded path disabled first Not Defined
shared library list:
dynamic /usr/lib/libpthread.1
dynamic /usr/lib/libc.2
shared library binding:
deferred
global hash table disabled ...
Example 2
Specifying -lc before -lpthread in threaded applications can cause run-time problems as in the following example.
Because the pthread/cma stubs are resolved instead of the real pthread/cma functions:
calls to pthread functions fail, due to uninitialized internal structures
calls to gethostbyname(3N) fail and return null
Apache webmin and perl DBI applications fail with "Can't load '<libname.sl>' for module <xxx>: Invalid
argument at <address>"
calls to shl_load(3x) fail with errno 22 (invalid argument) because the pthread_mutex_lock stub returns zero
$ cat a.c
#include <stdio.h>
#include <dl.h>
extern int errno;
main()
{
shl_load("/usr/lib/librt.2", BIND_DEFERRED, 0);
printf("Error %d, %s\n", errno, strerror(errno));
}
$ cc a.c -lc -lpthread
$ a.out
Error 22, Invalid argument
$ LD_PRELOAD=/usr/lib/libpthread.1 ./a.out
Error 0, Error 0
$ cat b.c
#include <stdio.h>
#include <dlfcn.h>
void* handle;
extern int errno;
main()
{
handle = dlopen("lib_not_found", RTLD_LAZY);
printf("Error %d, %s\n", errno, strerror(errno));
if (handle == NULL) {
printf("Error: %s\n",dlerror());
}
}
$ cc b.c -lc -lpthread
$ a.out
Error 22, Invalid argument
Error:
$ LD_PRELOAD=/usr/lib/libpthread.1 ./a.out
Error 0, Error 0
Error: Can't open shared library: lib_not_found
Therefore, -lc should never be specified in the build command of an executable or shared library.