1272343Sngie/*- 2272343Sngie * Copyright (c) 2011 The NetBSD Foundation, Inc. 3272343Sngie * All rights reserved. 4272343Sngie * 5272343Sngie * This code is derived from software contributed to The NetBSD Foundation 6272343Sngie * by Joerg Sonnenberger. 7272343Sngie * 8272343Sngie * Redistribution and use in source and binary forms, with or without 9272343Sngie * modification, are permitted provided that the following conditions 10272343Sngie * are met: 11272343Sngie * 1. Redistributions of source code must retain the above copyright 12272343Sngie * notice, this list of conditions and the following disclaimer. 13272343Sngie * 2. Redistributions in binary form must reproduce the above copyright 14272343Sngie * notice, this list of conditions and the following disclaimer in the 15272343Sngie * documentation and/or other materials provided with the distribution. 16272343Sngie * 17272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 18272343Sngie * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 19272343Sngie * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 20272343Sngie * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 21272343Sngie * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22272343Sngie * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23272343Sngie * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25272343Sngie * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26272343Sngie * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27272343Sngie * POSSIBILITY OF SUCH DAMAGE. 28272343Sngie */ 29272343Sngie 30272343Sngie#include <dlfcn.h> 31272343Sngie#include <link_elf.h> 32272343Sngie#include <pthread.h> 33272343Sngie#include <stdlib.h> 34272343Sngie#include <stdio.h> 35272343Sngie#include <string.h> 36272343Sngie#include <unistd.h> 37272343Sngie 38272343Sngieint sleep_init; 39272343Sngieint sleep_fini; 40272343Sngieint dlopen_cookie; 41272343Sngieint dlclose_cookie; 42272343Sngie 43272343Sngievoid (*tls_callback_sym)(void); 44272343Sngie 45272343Sngiestatic int 46272343Sngiedl_iterate_phdr_cb(struct dl_phdr_info *info, size_t size, void *data) 47272343Sngie{ 48272343Sngie (*tls_callback_sym)(); 49272343Sngie return 0; 50272343Sngie} 51272343Sngie 52272343Sngiestatic void * 53272343Sngietest_dl_iterate_phdr_helper(void *dummy) 54272343Sngie{ 55272343Sngie sleep(10); 56272343Sngie _exit(1); 57272343Sngie} 58272343Sngie 59272343Sngiestatic void 60272343Sngietest_dl_iterate_phdr(void) 61272343Sngie{ 62272343Sngie pthread_t t; 63272343Sngie void *dso; 64272343Sngie sleep_init = 0; 65272343Sngie sleep_fini = 0; 66272343Sngie if ((dso = dlopen("libh_helper_dso2.so", RTLD_LAZY)) == NULL) { 67272343Sngie fprintf(stderr, "opening helper failed\n"); 68272343Sngie _exit(1); 69272343Sngie } 70272343Sngie tls_callback_sym = dlsym(dso, "tls_callback"); 71272343Sngie if (tls_callback_sym == NULL) { 72272343Sngie fprintf(stderr, "bad helper\n"); 73272343Sngie _exit(1); 74272343Sngie } 75272343Sngie pthread_create(&t, NULL, test_dl_iterate_phdr_helper, NULL); 76272343Sngie if (dl_iterate_phdr(dl_iterate_phdr_cb, NULL)) 77272343Sngie _exit(1); 78272343Sngie _exit(0); 79272343Sngie} 80272343Sngie 81272343Sngiestatic void * 82272343Sngieinit_fini_helper(void *arg) 83272343Sngie{ 84272343Sngie void *dso; 85272343Sngie if ((dso = dlopen(arg, RTLD_LAZY)) == NULL) { 86272343Sngie fprintf(stderr, "opening %s failed\n", (char *)arg); 87272343Sngie exit(1); 88272343Sngie } 89272343Sngie dlclose(dso); 90272343Sngie return NULL; 91272343Sngie} 92272343Sngie 93272343Sngiestatic void 94272343Sngietest_dlopen(void) 95272343Sngie{ 96272343Sngie pthread_t t1, t2; 97272343Sngie sleep_init = 1; 98272343Sngie sleep_fini = 0; 99272343Sngie printf("%d\n", dlopen_cookie); 100272343Sngie pthread_create(&t1, NULL, init_fini_helper, 101272343Sngie __UNCONST("libh_helper_dso2.so")); 102272343Sngie sleep(1); 103272343Sngie printf("%d\n", dlopen_cookie); 104272343Sngie if (dlopen_cookie != 1) 105272343Sngie _exit(1); 106272343Sngie sleep(1); 107272343Sngie pthread_create(&t2, NULL, init_fini_helper, 108272343Sngie __UNCONST("libutil.so")); 109272343Sngie printf("%d\n", dlopen_cookie); 110272343Sngie if (dlopen_cookie != 1) 111272343Sngie _exit(1); 112272343Sngie _exit(0); 113272343Sngie} 114272343Sngie 115272343Sngiestatic void 116272343Sngietest_dlclose(void) 117272343Sngie{ 118272343Sngie pthread_t t1, t2; 119272343Sngie sleep_init = 0; 120272343Sngie sleep_fini = 1; 121272343Sngie printf("%d\n", dlclose_cookie); 122272343Sngie pthread_create(&t1, NULL, init_fini_helper, 123272343Sngie __UNCONST("libh_helper_dso2.so")); 124272343Sngie sleep(1); 125272343Sngie printf("%d\n", dlclose_cookie); 126272343Sngie if (dlclose_cookie != 2) 127272343Sngie _exit(1); 128272343Sngie pthread_create(&t2, NULL, init_fini_helper, 129272343Sngie __UNCONST("libutil.so")); 130272343Sngie sleep(1); 131272343Sngie printf("%d\n", dlclose_cookie); 132272343Sngie if (dlclose_cookie != 2) 133272343Sngie _exit(1); 134272343Sngie _exit(0); 135272343Sngie} 136272343Sngie 137272343Sngieint 138272343Sngiemain(int argc, char **argv) 139272343Sngie{ 140272343Sngie if (argc != 2) 141272343Sngie return 1; 142272343Sngie if (strcmp(argv[1], "dl_iterate_phdr") == 0) 143272343Sngie test_dl_iterate_phdr(); 144272343Sngie if (strcmp(argv[1], "dlopen") == 0) 145272343Sngie test_dlopen(); 146272343Sngie if (strcmp(argv[1], "dlclose") == 0) 147272343Sngie test_dlclose(); 148272343Sngie return 1; 149272343Sngie} 150