Home | History | Annotate | Line # | Download | only in ld.elf_so
t_ifunc.c revision 1.1.2.1
      1  1.1.2.1  pgoyette /*	$NetBSD: t_ifunc.c,v 1.1.2.1 2017/03/20 06:58:00 pgoyette Exp $	*/
      2      1.1     joerg 
      3      1.1     joerg /*
      4      1.1     joerg  * Copyright (c) 2014 The NetBSD Foundation, Inc.
      5      1.1     joerg  * All rights reserved.
      6      1.1     joerg  *
      7      1.1     joerg  * Redistribution and use in source and binary forms, with or without
      8      1.1     joerg  * modification, are permitted provided that the following conditions
      9      1.1     joerg  * are met:
     10      1.1     joerg  * 1. Redistributions of source code must retain the above copyright
     11      1.1     joerg  *    notice, this list of conditions and the following disclaimer.
     12      1.1     joerg  * 2. Redistributions in binary form must reproduce the above copyright
     13      1.1     joerg  *    notice, this list of conditions and the following disclaimer in the
     14      1.1     joerg  *    documentation and/or other materials provided with the distribution.
     15      1.1     joerg  *
     16      1.1     joerg  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
     17      1.1     joerg  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
     18      1.1     joerg  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     19      1.1     joerg  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20      1.1     joerg  * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
     21      1.1     joerg  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     22      1.1     joerg  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
     23      1.1     joerg  * 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
     25      1.1     joerg  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26      1.1     joerg  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
     27      1.1     joerg  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28      1.1     joerg  */
     29      1.1     joerg 
     30      1.1     joerg #include <sys/types.h>
     31      1.1     joerg 
     32      1.1     joerg #include <atf-c.h>
     33      1.1     joerg #include <dlfcn.h>
     34      1.1     joerg #include <util.h>
     35      1.1     joerg 
     36  1.1.2.1  pgoyette #include "h_macros.h"
     37      1.1     joerg 
     38      1.1     joerg ATF_TC(rtld_ifunc);
     39      1.1     joerg 
     40      1.1     joerg ATF_TC_HEAD(rtld_ifunc, tc)
     41      1.1     joerg {
     42      1.1     joerg 	atf_tc_set_md_var(tc, "descr", "ifunc functions are resolved");
     43      1.1     joerg }
     44      1.1     joerg 
     45      1.1     joerg ATF_TC_BODY(rtld_ifunc, tc)
     46      1.1     joerg {
     47      1.1     joerg 	const char *envstr[] = {
     48      1.1     joerg 	    "0", "1"
     49      1.1     joerg 	};
     50      1.1     joerg 	int expected_result[] = {
     51      1.1     joerg 	    0xdeadbeef, 0xbeefdead
     52      1.1     joerg 	};
     53      1.1     joerg 	void *handle;
     54      1.1     joerg 	int (*sym)(void);
     55      1.1     joerg 	int result;
     56      1.1     joerg 	const char *error;
     57      1.1     joerg 	size_t i;
     58      1.1     joerg 
     59      1.1     joerg 	for (i = 0; i < __arraycount(envstr); ++i) {
     60      1.1     joerg 		setenv("USE_IFUNC2", envstr[i], 1);
     61      1.1     joerg 
     62      1.1     joerg 		handle = dlopen("libh_helper_ifunc_dso.so", RTLD_LAZY);
     63      1.1     joerg 		error = dlerror();
     64      1.1     joerg 		ATF_CHECK(error == NULL);
     65      1.1     joerg 		ATF_CHECK(handle != NULL);
     66      1.1     joerg 
     67      1.1     joerg 		sym = dlsym(handle, "ifunc");
     68      1.1     joerg 		error = dlerror();
     69      1.1     joerg 		ATF_CHECK(error == NULL);
     70      1.1     joerg 		ATF_CHECK(sym != NULL);
     71      1.1     joerg 
     72      1.1     joerg 		result = (*sym)();
     73      1.1     joerg 		ATF_CHECK(result == expected_result[i]);
     74      1.1     joerg 
     75      1.1     joerg 		dlclose(handle);
     76      1.1     joerg 		error = dlerror();
     77      1.1     joerg 		ATF_CHECK(error == NULL);
     78      1.1     joerg 
     79      1.1     joerg 		char *command;
     80      1.1     joerg 		easprintf(&command, "%s/h_ifunc %d",
     81      1.1     joerg 		    atf_tc_get_config_var(tc, "srcdir"), expected_result[i]);
     82      1.1     joerg 		if (system(command) != EXIT_SUCCESS)
     83      1.1     joerg 			atf_tc_fail("Test failed; see output for details");
     84      1.1     joerg 		free(command);
     85      1.1     joerg 	}
     86      1.1     joerg }
     87      1.1     joerg 
     88      1.1     joerg ATF_TP_ADD_TCS(tp)
     89      1.1     joerg {
     90      1.1     joerg 	ATF_TP_ADD_TC(tp, rtld_ifunc);
     91      1.1     joerg 	return 0;
     92      1.1     joerg }
     93