[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Shared libraries and posix threads question
- To: misc_(_at_)_openbsd_(_dot_)_org
- Subject: Re: Shared libraries and posix threads question
- From: Tzvetan Chaliavski <ceco_(_at_)_commandcom_(_dot_)_com>
- Date: Tue, 28 Aug 2001 17:37:58 -0400
- Organization: Command Software Systems, Inc
Never mind the question. I figured out how to make it work. You should
include the -lc_r library before you link with the user specified
library.
Thanx anyways ;-) I'm out of this list.
Tzvetan Chaliavski wrote:
>
> Hi all,
>
> Does anyone know if OpenBSD allows invocation of pthread_xxxx functions
> from within shared library initialization function (e.g. _init())?
> I have piece of code that relies on some data being initialized when
> the shared library is lodaded. The following fragment (below) ilustrates
> the logic. Now after I've compiled and linked everything if I try to
> run test I get:
>
> $ Memory fault (core dumped)
>
> I've found out that the problem comes from the line:
>
> pthread_mutex_lock(&mtx);
>
> which seems to be failing. I was also surprised to find out that
> OpenBSD does not support attribute declarations like:
>
> void _init(void) __attribute__((section(".init")));
>
> However, if you have single function named _init() it will still
> be invoked during the shared object initialization. It would seem though
> that the _fini() function is never called. Is that the expected
> behaviour?
> If so - does anyone know how to code shared library that relies on these
> two functions being properly called. I've performed all tests on
> OpenBSD v2.9. Any suggestions are welcome,
>
> ceco
>
> --------------- libmain.c -----------------------
> #include <stdio.h>
> #include <pthread.h>
>
> pthread_mutex_t mtx;
>
> void _init(void)
> {
> printf("_init() called\n");
> pthread_mutex_init(&mtx, NULL);
> pthread_mutex_lock(&mtx);
> pthread_mutex_unlock(&mtx);
> }
>
> void _fini(void)
> {
> printf("_fini() called\n");
> pthread_mutex_destroy(&mtx);
> }
>
> void func(void)
> {
> pthread_mutex_lock(&mtx);
> printf("func() called\n");
> pthread_mutex_unlock(&mtx);
> }
> ----------------------------------------------
>
> --------- test.c --------
> #include <stdio.h>
>
> extern void func(void);
>
> int main()
> {
> printf("calling func()\n");
> func();
> printf("done\n");
> return 0;
> }
> -----------------------------
>
> -------- Makefile ---------
>
> test: test.c libfoo.so
> gcc -pthread -o test test.c libfoo.so
>
> libfoo.so: libmain.c
> gcc -pthread -c -fPIC -o libmain.o libmain.c
> ld -Bforcearchive -Bdynamic -shared -o libfoo.so libmain.o
>
> clean:
> rm -f *.so
> rm -f *.o
> rm -f test
> rm -f test.core
> ----------------------------------
>
> --
> Tzvetan Chaliavski Command Software Systems, Inc.
> ceco_(_at_)_commandcom_(_dot_)_com 1061 E. Indiantown Road
> Phone (561)575-3200 x4197 Jupiter
> Fax (561)575-3026 FL 33477
--
@*** Flap's Law ***
Any inanimate object, regardless of its position or
configuration, may be expected to perform at any time
in a totally unexpected manner for reasons that are
either entirely obscure or else completely mysterious.
--
Tzvetan Chaliavski Command Software Systems, Inc.
ceco_(_at_)_commandcom_(_dot_)_com 1061 E. Indiantown Road
Phone (561)575-3200 x4197 Jupiter
Fax (561)575-3026 FL 33477
Visit your host, monkey.org