1272343Sngie/*	$NetBSD: t_ifunc.c,v 1.1 2014/08/25 20:40:53 joerg Exp $	*/
2272343Sngie
3272343Sngie/*
4272343Sngie * Copyright (c) 2014 The NetBSD Foundation, Inc.
5272343Sngie * All rights reserved.
6272343Sngie *
7272343Sngie * Redistribution and use in source and binary forms, with or without
8272343Sngie * modification, are permitted provided that the following conditions
9272343Sngie * are met:
10272343Sngie * 1. Redistributions of source code must retain the above copyright
11272343Sngie *    notice, this list of conditions and the following disclaimer.
12272343Sngie * 2. Redistributions in binary form must reproduce the above copyright
13272343Sngie *    notice, this list of conditions and the following disclaimer in the
14272343Sngie *    documentation and/or other materials provided with the distribution.
15272343Sngie *
16272343Sngie * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND
17272343Sngie * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
18272343Sngie * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19272343Sngie * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20272343Sngie * IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY
21272343Sngie * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22272343Sngie * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
23272343Sngie * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24272343Sngie * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
25272343Sngie * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26272343Sngie * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27272343Sngie * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28272343Sngie */
29272343Sngie
30272343Sngie#include <sys/types.h>
31272343Sngie
32272343Sngie#include <atf-c.h>
33272343Sngie#include <dlfcn.h>
34272343Sngie#include <util.h>
35272343Sngie
36272343Sngie#include "../../h_macros.h"
37272343Sngie
38272343SngieATF_TC(rtld_ifunc);
39272343Sngie
40272343SngieATF_TC_HEAD(rtld_ifunc, tc)
41272343Sngie{
42272343Sngie	atf_tc_set_md_var(tc, "descr", "ifunc functions are resolved");
43272343Sngie}
44272343Sngie
45272343SngieATF_TC_BODY(rtld_ifunc, tc)
46272343Sngie{
47272343Sngie	const char *envstr[] = {
48272343Sngie	    "0", "1"
49272343Sngie	};
50272343Sngie	int expected_result[] = {
51272343Sngie	    0xdeadbeef, 0xbeefdead
52272343Sngie	};
53272343Sngie	void *handle;
54272343Sngie	int (*sym)(void);
55272343Sngie	int result;
56272343Sngie	const char *error;
57272343Sngie	size_t i;
58272343Sngie
59272343Sngie	for (i = 0; i < __arraycount(envstr); ++i) {
60272343Sngie		setenv("USE_IFUNC2", envstr[i], 1);
61272343Sngie
62272343Sngie		handle = dlopen("libh_helper_ifunc_dso.so", RTLD_LAZY);
63272343Sngie		error = dlerror();
64272343Sngie		ATF_CHECK(error == NULL);
65272343Sngie		ATF_CHECK(handle != NULL);
66272343Sngie
67272343Sngie		sym = dlsym(handle, "ifunc");
68272343Sngie		error = dlerror();
69272343Sngie		ATF_CHECK(error == NULL);
70272343Sngie		ATF_CHECK(sym != NULL);
71272343Sngie
72272343Sngie		result = (*sym)();
73272343Sngie		ATF_CHECK(result == expected_result[i]);
74272343Sngie
75272343Sngie		dlclose(handle);
76272343Sngie		error = dlerror();
77272343Sngie		ATF_CHECK(error == NULL);
78272343Sngie
79272343Sngie		char *command;
80272343Sngie		easprintf(&command, "%s/h_ifunc %d",
81272343Sngie		    atf_tc_get_config_var(tc, "srcdir"), expected_result[i]);
82272343Sngie		if (system(command) != EXIT_SUCCESS)
83272343Sngie			atf_tc_fail("Test failed; see output for details");
84272343Sngie		free(command);
85272343Sngie	}
86272343Sngie}
87272343Sngie
88272343SngieATF_TP_ADD_TCS(tp)
89272343Sngie{
90272343Sngie	ATF_TP_ADD_TC(tp, rtld_ifunc);
91272343Sngie	return 0;
92272343Sngie}
93