Home | History | Annotate | Line # | Download | only in ld.elf_so
      1  1.1  joerg /*-
      2  1.1  joerg  * Copyright (c) 2017 The NetBSD Foundation, Inc.
      3  1.1  joerg  * All rights reserved.
      4  1.1  joerg  *
      5  1.1  joerg  * This code is derived from software contributed to The NetBSD Foundation
      6  1.1  joerg  * by Joerg Sonnenberger.
      7  1.1  joerg  *
      8  1.1  joerg  * Redistribution and use in source and binary forms, with or without
      9  1.1  joerg  * modification, are permitted provided that the following conditions
     10  1.1  joerg  * are met:
     11  1.1  joerg  * 1. Redistributions of source code must retain the above copyright
     12  1.1  joerg  *    notice, this list of conditions and the following disclaimer.
     13  1.1  joerg  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  joerg  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  joerg  *    documentation and/or other materials provided with the distribution.
     16  1.1  joerg  *
     17  1.1  joerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     18  1.1  joerg  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1  joerg  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1  joerg  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     21  1.1  joerg  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     22  1.1  joerg  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     23  1.1  joerg  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     24  1.1  joerg  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     25  1.1  joerg  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     26  1.1  joerg  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     27  1.1  joerg  * POSSIBILITY OF SUCH DAMAGE.
     28  1.1  joerg  */
     29  1.1  joerg 
     30  1.1  joerg #include <dlfcn.h>
     31  1.1  joerg #include <err.h>
     32  1.1  joerg #include <pthread.h>
     33  1.1  joerg #include <stdio.h>
     34  1.1  joerg 
     35  1.1  joerg static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
     36  1.1  joerg static pthread_cond_t cond1 = PTHREAD_COND_INITIALIZER;
     37  1.1  joerg static pthread_cond_t cond2 = PTHREAD_COND_INITIALIZER;
     38  1.1  joerg 
     39  1.1  joerg static void *
     40  1.1  joerg thread_helper(void *arg)
     41  1.1  joerg {
     42  1.1  joerg 	void (*testfunc)(void) = arg;
     43  1.1  joerg 	testfunc();
     44  1.1  joerg 
     45  1.1  joerg 	pthread_mutex_lock(&mutex);
     46  1.1  joerg 	pthread_cond_broadcast(&cond1);
     47  1.1  joerg 	pthread_mutex_unlock(&mutex);
     48  1.1  joerg 
     49  1.1  joerg 	pthread_mutex_lock(&mutex);
     50  1.1  joerg 	pthread_cond_wait(&cond2, &mutex);
     51  1.1  joerg 	pthread_mutex_unlock(&mutex);
     52  1.1  joerg 
     53  1.1  joerg 	return NULL;
     54  1.1  joerg }
     55  1.1  joerg 
     56  1.1  joerg int
     57  1.1  joerg main(void)
     58  1.1  joerg {
     59  1.1  joerg 	void *dso;
     60  1.1  joerg 	void (*testfunc)(void);
     61  1.1  joerg 	pthread_t thread;
     62  1.1  joerg 
     63  1.1  joerg 	dso = dlopen("libh_helper_dso3.so", RTLD_LAZY);
     64  1.1  joerg 	if (dso == NULL)
     65  1.1  joerg 		errx(1, "%s", dlerror());
     66  1.1  joerg 	testfunc = dlsym(dso, "testfunc");
     67  1.1  joerg 	if (testfunc == NULL)
     68  1.1  joerg 		errx(1, "%s", dlerror());
     69  1.1  joerg 
     70  1.1  joerg 	pthread_mutex_lock(&mutex);
     71  1.1  joerg 
     72  1.1  joerg 	if (pthread_create(&thread, NULL, thread_helper, testfunc))
     73  1.1  joerg 		err(1, "pthread_create");
     74  1.1  joerg 
     75  1.1  joerg 	pthread_cond_wait(&cond1, &mutex);
     76  1.1  joerg 	pthread_mutex_unlock(&mutex);
     77  1.1  joerg 
     78  1.1  joerg 	printf("before dlclose\n");
     79  1.1  joerg 	dlclose(dso);
     80  1.1  joerg 	printf("after dlclose\n");
     81  1.1  joerg 	dso = dlopen("libh_helper_dso3.so", RTLD_LAZY);
     82  1.1  joerg 	if (dso == NULL)
     83  1.1  joerg 		errx(1, "%s", dlerror());
     84  1.1  joerg 	dlclose(dso);
     85  1.2  joerg 
     86  1.2  joerg 	pthread_mutex_lock(&mutex);
     87  1.2  joerg 	pthread_cond_signal(&cond2);
     88  1.2  joerg 	pthread_mutex_unlock(&mutex);
     89  1.2  joerg 
     90  1.1  joerg 	if (pthread_join(thread, NULL))
     91  1.1  joerg 		err(1, "pthread_join");
     92  1.1  joerg 	return 0;
     93  1.1  joerg }
     94